dsh-remote 0.8.94 → 1.1.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 (196) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +128 -370
  3. package/README.md +129 -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 +174 -172
  8. package/lib/boot.js +42 -42
  9. package/lib/client.js +6381 -4662
  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-changes.js +57 -0
  14. package/lib/file-reference.js +457 -457
  15. package/lib/forwards.js +258 -258
  16. package/lib/host-api.js +822 -822
  17. package/lib/hostkey.js +108 -108
  18. package/lib/http-transport.js +201 -201
  19. package/lib/ignore.js +202 -202
  20. package/lib/index.js +4555 -4488
  21. package/lib/instructions-dynamic.js +415 -415
  22. package/lib/instructions.js +288 -288
  23. package/lib/open-folder.js +56 -56
  24. package/lib/openssh.js +950 -940
  25. package/lib/paths.js +292 -292
  26. package/lib/pool.js +1029 -1029
  27. package/lib/posix-shell.cjs +34 -34
  28. package/lib/registry.js +296 -296
  29. package/lib/remote-fs.js +179 -173
  30. package/lib/remote-git.js +127 -0
  31. package/lib/remote-sandbox.js +183 -183
  32. package/lib/remote-shell-child.cjs +460 -397
  33. package/lib/remote-shell.js +697 -697
  34. package/lib/remote-skills.js +312 -312
  35. package/lib/remote-write.js +51 -0
  36. package/lib/routes-audit.js +58 -58
  37. package/lib/routes-forwards.js +72 -72
  38. package/lib/routes-fs.js +299 -251
  39. package/lib/routes-machines.js +217 -186
  40. package/lib/routes-misc.js +187 -187
  41. package/lib/routes-term.js +169 -169
  42. package/lib/routes-update.js +147 -143
  43. package/lib/routes-web-attach.js +121 -112
  44. package/lib/routes-workspace.js +325 -312
  45. package/lib/search.js +235 -235
  46. package/lib/self-heal.js +130 -130
  47. package/lib/sftp-surface.js +119 -119
  48. package/lib/sshconfig.js +779 -779
  49. package/lib/sync-status.js +31 -0
  50. package/lib/sync.js +350 -350
  51. package/lib/tasks.js +95 -95
  52. package/lib/telemetry.js +247 -247
  53. package/lib/terminal.js +205 -205
  54. package/lib/tui/flow.js +130 -130
  55. package/lib/tui/format.js +380 -380
  56. package/lib/tui/i18n.js +350 -350
  57. package/lib/tui/index.js +531 -531
  58. package/lib/tui/provider.js +344 -344
  59. package/lib/tui/scene.js +986 -986
  60. package/lib/update.js +497 -490
  61. package/lib/vendor/jsdiff/LICENSE +29 -0
  62. package/lib/vendor/jsdiff/SOURCE.md +4 -0
  63. package/lib/vendor/jsdiff/diff/base.js +254 -0
  64. package/lib/vendor/jsdiff/diff/line.js +66 -0
  65. package/lib/vendor/jsdiff/patch/create.js +229 -0
  66. package/lib/vendor/jsdiff/util/params.js +15 -0
  67. package/lib/web-attach.js +1081 -860
  68. package/lib/workspace-cleanup.js +337 -337
  69. package/lib/workspace-search.js +219 -0
  70. package/node_modules/buildcheck/.eslintrc.js +5 -0
  71. package/node_modules/buildcheck/.github/workflows/ci.yml +88 -0
  72. package/node_modules/buildcheck/.github/workflows/lint.yml +27 -0
  73. package/node_modules/buildcheck/.npmignore +3 -0
  74. package/node_modules/buildcheck/LICENSE +19 -0
  75. package/node_modules/buildcheck/README.md +106 -0
  76. package/node_modules/buildcheck/deps/Find-VisualStudio.cs +250 -0
  77. package/node_modules/buildcheck/lib/findvs.js +361 -0
  78. package/node_modules/buildcheck/lib/index.js +738 -0
  79. package/node_modules/buildcheck/package.json +36 -0
  80. package/node_modules/buildcheck/test/test.js +8 -0
  81. package/node_modules/cpu-features/.eslintrc.js +5 -0
  82. package/node_modules/cpu-features/.github/workflows/ci.yml +74 -0
  83. package/node_modules/cpu-features/.github/workflows/lint.yml +27 -0
  84. package/node_modules/cpu-features/LICENSE +19 -0
  85. package/node_modules/cpu-features/README.md +59 -0
  86. package/node_modules/cpu-features/binding.gyp +16 -0
  87. package/node_modules/cpu-features/buildcheck.js +32 -0
  88. package/node_modules/cpu-features/deps/cpu_features/.clang-format +4 -0
  89. package/node_modules/cpu-features/deps/cpu_features/.dockerignore +31 -0
  90. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/Dockerfile +5 -0
  91. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/aarch64_linux_cmake.yml +30 -0
  92. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_freebsd_cmake.yml +22 -0
  93. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_bazel.yml +26 -0
  94. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_cmake.yml +31 -0
  95. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_macos_cmake.yml +43 -0
  96. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_windows_cmake.yml +25 -0
  97. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/arm_linux_cmake.yml +31 -0
  98. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/clang_format.yml +24 -0
  99. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/mips_linux_cmake.yml +30 -0
  100. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/power_linux_cmake.yml +29 -0
  101. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/riscv_linux_cmake.yml +28 -0
  102. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/s390x_linux_cmake.yml +27 -0
  103. package/node_modules/cpu-features/deps/cpu_features/.grenrc.yml +21 -0
  104. package/node_modules/cpu-features/deps/cpu_features/BUILD.bazel +329 -0
  105. package/node_modules/cpu-features/deps/cpu_features/CMakeLists.txt +261 -0
  106. package/node_modules/cpu-features/deps/cpu_features/CONTRIBUTING.md +23 -0
  107. package/node_modules/cpu-features/deps/cpu_features/LICENSE +230 -0
  108. package/node_modules/cpu-features/deps/cpu_features/README.md +272 -0
  109. package/node_modules/cpu-features/deps/cpu_features/WORKSPACE +19 -0
  110. package/node_modules/cpu-features/deps/cpu_features/bazel/ci/README.md +5 -0
  111. package/node_modules/cpu-features/deps/cpu_features/bazel/platforms.bzl +11 -0
  112. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesConfig.cmake.in +3 -0
  113. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesNdkCompatConfig.cmake.in +3 -0
  114. package/node_modules/cpu-features/deps/cpu_features/cmake/README.md +30 -0
  115. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/Makefile +252 -0
  116. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/README.md +40 -0
  117. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.dot +64 -0
  118. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.svg +312 -0
  119. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/generate_image.sh +7 -0
  120. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/amd64/Dockerfile +48 -0
  121. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/toolchain/Dockerfile +34 -0
  122. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/CMakeLists.txt +22 -0
  123. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/main.cpp +11 -0
  124. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/vagrant/freebsd/Vagrantfile +107 -0
  125. package/node_modules/cpu-features/deps/cpu_features/cmake/googletest.CMakeLists.txt.in +15 -0
  126. package/node_modules/cpu-features/deps/cpu_features/cpu_features.gyp +105 -0
  127. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_cache_info.h +54 -0
  128. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_macros.h +384 -0
  129. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_aarch64.h +259 -0
  130. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_arm.h +121 -0
  131. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_mips.h +74 -0
  132. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_ppc.h +149 -0
  133. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_riscv.h +72 -0
  134. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_s390x.h +108 -0
  135. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_x86.h +288 -0
  136. package/node_modules/cpu-features/deps/cpu_features/include/internal/bit_utils.h +40 -0
  137. package/node_modules/cpu-features/deps/cpu_features/include/internal/cpuid_x86.h +37 -0
  138. package/node_modules/cpu-features/deps/cpu_features/include/internal/filesystem.h +39 -0
  139. package/node_modules/cpu-features/deps/cpu_features/include/internal/hwcaps.h +240 -0
  140. package/node_modules/cpu-features/deps/cpu_features/include/internal/stack_line_reader.h +49 -0
  141. package/node_modules/cpu-features/deps/cpu_features/include/internal/string_view.h +110 -0
  142. package/node_modules/cpu-features/deps/cpu_features/include/internal/windows_utils.h +70 -0
  143. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/CMakeLists.txt +60 -0
  144. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/README.md +4 -0
  145. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.c +205 -0
  146. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.h +320 -0
  147. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/ndk-compat-test.c +12 -0
  148. package/node_modules/cpu-features/deps/cpu_features/patches/0001-Add-Apple-Silicon-Support.patch +456 -0
  149. package/node_modules/cpu-features/deps/cpu_features/scripts/generate_badges.d +165 -0
  150. package/node_modules/cpu-features/deps/cpu_features/scripts/make_release.sh +75 -0
  151. package/node_modules/cpu-features/deps/cpu_features/scripts/run_integration.sh +490 -0
  152. package/node_modules/cpu-features/deps/cpu_features/scripts/test_integration.sh +79 -0
  153. package/node_modules/cpu-features/deps/cpu_features/src/copy.inl +19 -0
  154. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection.inl +86 -0
  155. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection_and_hwcaps.inl +26 -0
  156. package/node_modules/cpu-features/deps/cpu_features/src/equals.inl +22 -0
  157. package/node_modules/cpu-features/deps/cpu_features/src/filesystem.c +62 -0
  158. package/node_modules/cpu-features/deps/cpu_features/src/hwcaps.c +169 -0
  159. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64__base_implementation.inl +88 -0
  160. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_linux_or_android.c +79 -0
  161. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_macos_or_iphone.c +82 -0
  162. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_windows.c +138 -0
  163. package/node_modules/cpu-features/deps/cpu_features/src/impl_arm_linux_or_android.c +212 -0
  164. package/node_modules/cpu-features/deps/cpu_features/src/impl_mips_linux_or_android.c +93 -0
  165. package/node_modules/cpu-features/deps/cpu_features/src/impl_ppc_linux.c +163 -0
  166. package/node_modules/cpu-features/deps/cpu_features/src/impl_riscv_linux.c +111 -0
  167. package/node_modules/cpu-features/deps/cpu_features/src/impl_s390x_linux.c +120 -0
  168. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86__base_implementation.inl +2055 -0
  169. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_freebsd.c +68 -0
  170. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_linux_or_android.c +58 -0
  171. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_macos.c +57 -0
  172. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_windows.c +58 -0
  173. package/node_modules/cpu-features/deps/cpu_features/src/stack_line_reader.c +132 -0
  174. package/node_modules/cpu-features/deps/cpu_features/src/string_view.c +192 -0
  175. package/node_modules/cpu-features/deps/cpu_features/src/utils/list_cpu_features.c +461 -0
  176. package/node_modules/cpu-features/deps/cpu_features/test/CMakeLists.txt +114 -0
  177. package/node_modules/cpu-features/deps/cpu_features/test/bit_utils_test.cc +53 -0
  178. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_aarch64_test.cc +396 -0
  179. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_arm_test.cc +384 -0
  180. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_mips_test.cc +176 -0
  181. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_ppc_test.cc +131 -0
  182. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_riscv_test.cc +180 -0
  183. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_s390x_test.cc +82 -0
  184. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_x86_test.cc +1729 -0
  185. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.cc +103 -0
  186. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.h +61 -0
  187. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.cc +52 -0
  188. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.h +31 -0
  189. package/node_modules/cpu-features/deps/cpu_features/test/stack_line_reader_test.cc +132 -0
  190. package/node_modules/cpu-features/deps/cpu_features/test/string_view_test.cc +202 -0
  191. package/node_modules/cpu-features/lib/index.js +5 -0
  192. package/node_modules/cpu-features/package.json +40 -0
  193. package/node_modules/cpu-features/src/binding.cc +151 -0
  194. package/node_modules/cpu-features/test/test.js +12 -0
  195. package/node_modules/nan/tools/1to2.js +0 -0
  196. package/package.json +107 -103
package/lib/audit.js CHANGED
@@ -1,45 +1,45 @@
1
- // dsh-remote — audit log domain: the pure parts (formatting + reading).
2
- //
3
- // Extracted from lib/index.js (issue #82): the god-file keeps only the thin
4
- // wiring (config gate + file path); everything testable without a mounted
5
- // plugin lives here. Behaviour is byte-identical to the inlined version —
6
- // `audit-open-and-no-deploy.test.js` pins it end to end.
7
-
8
- /**
9
- * Build one audit line (without the trailing newline handling of the caller).
10
- * `cmd` is flattened to a single line and capped at 400 chars.
11
- *
12
- * @param {object} p
13
- * @param {string} p.op - operation name.
14
- * @param {string} p.cmd - command text or path detail.
15
- * @param {number|null} p.code - exit code, or null when not applicable.
16
- * @param {string} [p.user]
17
- * @param {string} [p.host]
18
- * @param {number} [p.port]
19
- * @param {Date} [p.now] - clock injection for tests.
20
- * @returns {string} the line INCLUDING the trailing newline.
21
- */
22
- export function formatAuditLine({ op, cmd, code, user, host, port, now }) {
23
- const iso = (now || new Date()).toISOString()
24
- const flat = String(cmd || '').replace(/\s+/g, ' ').slice(0, 400)
25
- return [iso, `${user || '?'}@${host || '?'}:${port}`, op, code == null ? '-' : String(code), flat].join(' | ') + '\n'
26
- }
27
-
28
- /**
29
- * Read the last `limit` audit lines from a file (clamped to [1, 500]).
30
- * A missing/corrupt file degrades to [] — the audit viewer must never throw.
31
- *
32
- * @param {(file: string, enc: string) => string} readFile - fs.readFileSync-compatible.
33
- * @param {string} file
34
- * @param {number|string} [limit]
35
- * @returns {string[]}
36
- */
37
- export function readAuditLines(readFile, file, limit) {
38
- try {
39
- const text = readFile(file, 'utf8')
40
- const lines = text.split('\n').filter(Boolean)
41
- return lines.slice(-Math.max(1, Math.min(Number(limit) || 50, 500)))
42
- } catch {
43
- return []
44
- }
45
- }
1
+ // dsh-remote — audit log domain: the pure parts (formatting + reading).
2
+ //
3
+ // Extracted from lib/index.js (issue #82): the god-file keeps only the thin
4
+ // wiring (config gate + file path); everything testable without a mounted
5
+ // plugin lives here. Behaviour is byte-identical to the inlined version —
6
+ // `audit-open-and-no-deploy.test.js` pins it end to end.
7
+
8
+ /**
9
+ * Build one audit line (without the trailing newline handling of the caller).
10
+ * `cmd` is flattened to a single line and capped at 400 chars.
11
+ *
12
+ * @param {object} p
13
+ * @param {string} p.op - operation name.
14
+ * @param {string} p.cmd - command text or path detail.
15
+ * @param {number|null} p.code - exit code, or null when not applicable.
16
+ * @param {string} [p.user]
17
+ * @param {string} [p.host]
18
+ * @param {number} [p.port]
19
+ * @param {Date} [p.now] - clock injection for tests.
20
+ * @returns {string} the line INCLUDING the trailing newline.
21
+ */
22
+ export function formatAuditLine({ op, cmd, code, user, host, port, now }) {
23
+ const iso = (now || new Date()).toISOString()
24
+ const flat = String(cmd || '').replace(/\s+/g, ' ').slice(0, 400)
25
+ return [iso, `${user || '?'}@${host || '?'}:${port}`, op, code == null ? '-' : String(code), flat].join(' | ') + '\n'
26
+ }
27
+
28
+ /**
29
+ * Read the last `limit` audit lines from a file (clamped to [1, 500]).
30
+ * A missing/corrupt file degrades to [] — the audit viewer must never throw.
31
+ *
32
+ * @param {(file: string, enc: string) => string} readFile - fs.readFileSync-compatible.
33
+ * @param {string} file
34
+ * @param {number|string} [limit]
35
+ * @returns {string[]}
36
+ */
37
+ export function readAuditLines(readFile, file, limit) {
38
+ try {
39
+ const text = readFile(file, 'utf8')
40
+ const lines = text.split('\n').filter(Boolean)
41
+ return lines.slice(-Math.max(1, Math.min(Number(limit) || 50, 500)))
42
+ } catch {
43
+ return []
44
+ }
45
+ }
@@ -0,0 +1,70 @@
1
+ // Automatic-update orchestration. Package replacement and loader reload remain
2
+ // in update.js; this owner controls mode changes, overlap and retry timing.
3
+ export function createAutoUpdater({ mode, isInstalled, probe, apply, diskVersion, loadedVersion, gtVersion, reload, autoReload = () => true, intervalMs, now = Date.now, setTimer = setInterval, clearTimer = clearInterval }) {
4
+ const interval = Math.max(Number(intervalMs) || 1800000, 60000)
5
+ let stopped = false, timer = null, running = null, generation = 0, failures = 0, nextCheckAt = 0
6
+ let state = { phase: 'idle', checkedAt: null, error: '', latest: null }
7
+ const active = () => !stopped && mode() === 'auto' && isInstalled()
8
+ const snapshot = () => ({ ...state, mode: mode(), intervalMs: interval, nextCheckAt: nextCheckAt || null })
9
+ const check = ({ force = false } = {}) => {
10
+ if (running) return running
11
+ if (!active()) { state = { ...state, phase: isInstalled() ? 'paused' : 'unsupported' }; return Promise.resolve(snapshot()) }
12
+ if (!force && now() < nextCheckAt) return Promise.resolve(snapshot())
13
+ const ticket = generation
14
+ const current = () => active() && ticket === generation
15
+ const job = Promise.resolve().then(async () => {
16
+ if (!current()) return
17
+ state = { ...state, phase: 'checking', checkedAt: now(), error: '' }
18
+ nextCheckAt = now() + interval
19
+ try {
20
+ // A package installed by the CLI can already be on disk. It still
21
+ // needs a loader swap, even when npm has no version newer than disk.
22
+ if (gtVersion(diskVersion(), loadedVersion)) {
23
+ if (autoReload()) { reload(); state = { ...state, phase: 'reloading' } }
24
+ else state = { ...state, phase: 'restart-required' }
25
+ return
26
+ }
27
+ const result = await probe()
28
+ if (!current()) return
29
+ if (result.error) throw new Error(result.error)
30
+ state = { ...state, latest: result.version }
31
+ if (!gtVersion(result.version, diskVersion()) && !gtVersion(result.version, loadedVersion)) {
32
+ failures = 0
33
+ state = { ...state, phase: 'current' }; return
34
+ }
35
+ state = { ...state, phase: 'downloading' }
36
+ await apply(result.version)
37
+ if (!current()) return
38
+ failures = 0
39
+ if (autoReload()) { reload(); state = { ...state, phase: 'reloading' } }
40
+ else state = { ...state, phase: 'restart-required' }
41
+ } catch (error) {
42
+ if (!current()) return
43
+ failures++
44
+ nextCheckAt = now() + Math.min(60000 * 2 ** Math.min(failures - 1, 5), 1800000)
45
+ state = { ...state, phase: 'error', error: String(error.message || error) }
46
+ }
47
+ }).finally(() => { if (running === job) running = null })
48
+ running = job
49
+ return job.then(snapshot)
50
+ }
51
+ return {
52
+ snapshot,
53
+ check,
54
+ start() {
55
+ if (timer != null || stopped || !isInstalled()) return check()
56
+ // Cheap minute ticks allow bounded retry backoff without turning the
57
+ // configured successful-check interval into repeated network traffic.
58
+ timer = setTimer(() => { void check() }, Math.min(interval, 60000))
59
+ timer?.unref?.()
60
+ return check()
61
+ },
62
+ modeChanged() {
63
+ generation++; failures = 0; nextCheckAt = 0
64
+ if (mode() !== 'auto') { state = { ...state, phase: 'paused' }; return Promise.resolve(snapshot()) }
65
+ const previous = running
66
+ return previous ? previous.then(() => check({ force: true })) : check({ force: true })
67
+ },
68
+ stop() { stopped = true; generation++; if (timer != null) clearTimer(timer); timer = null },
69
+ }
70
+ }
package/lib/binding.js CHANGED
@@ -1,172 +1,174 @@
1
- // Session → remote-binding resolution.
2
- //
3
- // A session's remote target is derived from its own workspace mirror, never
4
- // from the plugin's mutable active-machine state. That is what keeps concurrent
5
- // sessions on different hosts independent: with a single shared connection and
6
- // a single shared workspace value, one session switching machines redirected
7
- // every other session's commands to the wrong host (issue #25).
8
- //
9
- // The mirror directory is the durable record of that binding: `ensureMirror()`
10
- // writes `.dsh-remote-meta.json` with the remote origin (`host`/`port`/
11
- // `username`/`remotePath`) when the workspace is picked, so a session's cwd
12
- // alone identifies both the machine to connect to and the workspace root.
13
-
14
- import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
15
- import path from 'node:path'
16
- import zlib from 'node:zlib'
17
-
18
- /** Strip trailing separators so a mirror root and a path inside it compare cleanly. */
19
- const norm = (p) => path.resolve(String(p || '')).replace(/[\\/]+$/, '') || ''
20
-
21
- /** Whether `base` is `dir` itself or nested under it (separator-aware, so
22
- * `/a/mirror-2` is NOT treated as living inside `/a/mirror`). */
23
- const isUnder = (base, dir) => base === dir || base.startsWith(dir + path.sep) || base.startsWith(dir + '/')
24
-
25
- /**
26
- * Resolve which remote workspace a LOCAL path belongs to by scanning the mirror
27
- * registry for the mirror that contains it.
28
- *
29
- * Mirrors live exactly one level under the registry root as
30
- * `<host>-<user>-<port>/<basename>`, and `mirrorDirFor()` appends a short hash
31
- * when two remote paths share a basename, so containment is unambiguous. The
32
- * longest containing path still wins, which keeps the result stable if a
33
- * future layout ever nests them.
34
- *
35
- * A mirror whose meta is missing, unparsable, or has no `host` is skipped
36
- * rather than guessed at: reporting no binding makes the caller refuse loudly,
37
- * while guessing would send commands to an unintended host.
38
- *
39
- * @param {string} local - absolute local path, typically a session's cwd.
40
- * @param {string} root - the mirror registry root (`$DSH_HOME/remote-workspaces`).
41
- * @returns {{mirrorDir: string|null, remotePath: string, machine: {host: string, port: number, username: string, alias: string}|null}}
42
- * `machine` is the mirror-recorded origin (`alias` is the ~/.ssh/config alias
43
- * it was created through, '' when the machine uses literal values), or null
44
- * when `local` is not inside a usable mirror (in which case `remotePath` is
45
- * empty too).
46
- */
47
- export function resolveMirror(local, root) {
48
- let mirrorDir = null
49
- let remotePath = ''
50
- let machine = null
51
- if (!local || !root || !existsSync(root)) return { mirrorDir, remotePath, machine }
52
- const base = norm(local)
53
- let hostDirs
54
- try {
55
- hostDirs = readdirSync(root)
56
- } catch {
57
- return { mirrorDir, remotePath, machine }
58
- }
59
- for (const hostDir of hostDirs) {
60
- const hostPath = path.join(root, hostDir)
61
- if (!statSync(hostPath, { throwIfNoEntry: false })?.isDirectory?.()) continue
62
- let entries
63
- try {
64
- entries = readdirSync(hostPath)
65
- } catch {
66
- continue
67
- }
68
- for (const entry of entries) {
69
- const metaPath = path.join(hostPath, entry, '.dsh-remote-meta.json')
70
- if (!existsSync(metaPath)) continue
71
- const mirrorAbs = norm(path.join(hostPath, entry))
72
- if (!isUnder(base, mirrorAbs)) continue
73
- // Deepest match wins; an equal-length path cannot occur twice.
74
- if (mirrorDir && mirrorAbs.length <= mirrorDir.length) continue
75
- try {
76
- const meta = JSON.parse(readFileSync(metaPath, 'utf8'))
77
- if (!meta || !meta.host) continue
78
- mirrorDir = mirrorAbs
79
- remotePath = String(meta.remotePath || '')
80
- machine = {
81
- host: String(meta.host),
82
- port: Number(meta.port) || 22,
83
- username: String(meta.username || ''),
84
- // The ~/.ssh/config alias this mirror was created through, when the
85
- // machine was saved as one (issue #38): it keeps the mirror bound to
86
- // its machine even if that alias's HostName/port later change.
87
- alias: String(meta.alias || ''),
88
- }
89
- } catch { /* unparsable meta → not a usable binding */ }
90
- }
91
- }
92
- return { mirrorDir, remotePath, machine }
93
- }
94
-
95
- /**
96
- * Stable pool key for a remote identity. Two sessions on the same machine
97
- * produce the same key and therefore share one SSH connection; sessions on
98
- * different machines never collide.
99
- * @param {{host: string, port?: number|string, username?: string}} m - the remote identity.
100
- * @returns {string} the canonical `user@host:port` key.
101
- */
102
- export function poolKey(m) {
103
- return `${(m && m.username) || ''}@${(m && m.host) || ''}:${Number(m && m.port) || 22}`
104
- }
105
-
106
- /** Safe path segment for a session id (mirrors DSH's encodeSegment). */
107
- export function encodeSegmentSafe(id) {
108
- const s = String(id || '').replace(/[^A-Za-z0-9._-]/g, '_')
109
- return s || 'session'
110
- }
111
-
112
- /** Read the cwd from a session log header (JSONL / .gz / zstd first frame). */
113
- export function readSessionHeaderCwd(file) {
114
- try {
115
- let buf = readFileSync(file)
116
- if (/\.zstd$/.test(file)) buf = zlib.zstdDecompressSync(buf)
117
- else if (/\.gz$/.test(file)) buf = zlib.gunzipSync(buf)
118
- const nl = buf.indexOf(10)
119
- if (nl < 0) return ''
120
- const head = JSON.parse(buf.subarray(0, nl).toString('utf8'))
121
- return typeof head.cwd === 'string' ? head.cwd : ''
122
- } catch {
123
- return ''
124
- }
125
- }
126
-
127
- /**
128
- * Resolve a session's workspace cwd from the live sessions service, then from
129
- * durable session logs under `$DSH_HOME/sessions`.
130
- */
131
- export function lookupSessionCwd(sessionId, { sessions, dshHome } = {}) {
132
- if (!sessionId) return ''
133
- try {
134
- if (sessions && typeof sessions.get === 'function') {
135
- const s = sessions.get(sessionId)
136
- const cwd = s && s.header && s.header.cwd
137
- if (cwd) return String(cwd)
138
- }
139
- } catch { /* sessions service unavailable */ }
140
- if (!dshHome) return ''
141
- try {
142
- const sessionsRoot = path.join(dshHome, 'sessions')
143
- if (!existsSync(sessionsRoot)) return ''
144
- const targetDir = encodeSegmentSafe(sessionId)
145
- for (const projDir of readdirSync(sessionsRoot)) {
146
- const projPath = path.join(sessionsRoot, projDir)
147
- if (!statSync(projPath, { throwIfNoEntry: false })?.isDirectory?.()) continue
148
- const sessDir = path.join(projPath, targetDir)
149
- if (!statSync(sessDir, { throwIfNoEntry: false })?.isDirectory?.()) continue
150
- const logFile = ['session.jsonl.zstd', 'session.jsonl', 'session.jsonl.gz']
151
- .map((n) => path.join(sessDir, n)).find((p) => existsSync(p))
152
- if (!logFile) continue
153
- const cwd = readSessionHeaderCwd(logFile)
154
- if (cwd) return cwd
155
- }
156
- } catch { /* session log scan failed */ }
157
- return ''
158
- }
159
-
160
- /** Pull sessionId / local from a JSON HTTP request (query string + body). */
161
- export function requestSessionHint(req, body = {}) {
162
- let sessionId = ''
163
- let local = ''
164
- try {
165
- const q = new URL(req.url || '', 'http://localhost').searchParams
166
- sessionId = q.get('sessionId') ? decodeURIComponent(q.get('sessionId')) : ''
167
- local = q.get('local') ? decodeURIComponent(q.get('local')) : ''
168
- } catch { /* malformed url */ }
169
- if (!sessionId && body && body.sessionId) sessionId = String(body.sessionId)
170
- if (!local && body && body.local) local = String(body.local)
171
- return { sessionId: String(sessionId || '').trim(), local: String(local || '').trim() }
172
- }
1
+ // Session → remote-binding resolution.
2
+ //
3
+ // A session's remote target is derived from its own workspace mirror, never
4
+ // from the plugin's mutable active-machine state. That is what keeps concurrent
5
+ // sessions on different hosts independent: with a single shared connection and
6
+ // a single shared workspace value, one session switching machines redirected
7
+ // every other session's commands to the wrong host (issue #25).
8
+ //
9
+ // The mirror directory is the durable record of that binding: `ensureMirror()`
10
+ // writes `.dsh-remote-meta.json` with the remote origin (`host`/`port`/
11
+ // `username`/`remotePath`) when the workspace is picked, so a session's cwd
12
+ // alone identifies both the machine to connect to and the workspace root.
13
+
14
+ import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
15
+ import path from 'node:path'
16
+ import zlib from 'node:zlib'
17
+
18
+ /** Strip trailing separators so a mirror root and a path inside it compare cleanly. */
19
+ const norm = (p) => path.resolve(String(p || '')).replace(/[\\/]+$/, '') || ''
20
+
21
+ /** Whether `base` is `dir` itself or nested under it (separator-aware, so
22
+ * `/a/mirror-2` is NOT treated as living inside `/a/mirror`). */
23
+ const isUnder = (base, dir) => base === dir || base.startsWith(dir + path.sep) || base.startsWith(dir + '/')
24
+
25
+ /**
26
+ * Resolve which remote workspace a LOCAL path belongs to by scanning the mirror
27
+ * registry for the mirror that contains it.
28
+ *
29
+ * Mirrors live exactly one level under the registry root as
30
+ * `<host>-<user>-<port>/<basename>`, and `mirrorDirFor()` appends a short hash
31
+ * when two remote paths share a basename, so containment is unambiguous. The
32
+ * longest containing path still wins, which keeps the result stable if a
33
+ * future layout ever nests them.
34
+ *
35
+ * A mirror whose meta is missing, unparsable, or has no `host` is skipped
36
+ * rather than guessed at: reporting no binding makes the caller refuse loudly,
37
+ * while guessing would send commands to an unintended host.
38
+ *
39
+ * @param {string} local - absolute local path, typically a session's cwd.
40
+ * @param {string} root - the mirror registry root (`$DSH_HOME/remote-workspaces`).
41
+ * @returns {{mirrorDir: string|null, remotePath: string, machine: {host: string, port: number, username: string, alias: string}|null}}
42
+ * `machine` is the mirror-recorded origin (`alias` is the ~/.ssh/config alias
43
+ * it was created through, '' when the machine uses literal values), or null
44
+ * when `local` is not inside a usable mirror (in which case `remotePath` is
45
+ * empty too).
46
+ */
47
+ export function resolveMirror(local, root) {
48
+ let mirrorDir = null
49
+ let remotePath = ''
50
+ let machine = null
51
+ if (!local || !root || !existsSync(root)) return { mirrorDir, remotePath, machine }
52
+ const base = norm(local)
53
+ let hostDirs
54
+ try {
55
+ hostDirs = readdirSync(root)
56
+ } catch {
57
+ return { mirrorDir, remotePath, machine }
58
+ }
59
+ for (const hostDir of hostDirs) {
60
+ const hostPath = path.join(root, hostDir)
61
+ if (!statSync(hostPath, { throwIfNoEntry: false })?.isDirectory?.()) continue
62
+ let entries
63
+ try {
64
+ entries = readdirSync(hostPath)
65
+ } catch {
66
+ continue
67
+ }
68
+ for (const entry of entries) {
69
+ const metaPath = path.join(hostPath, entry, '.dsh-remote-meta.json')
70
+ if (!existsSync(metaPath)) continue
71
+ const mirrorAbs = norm(path.join(hostPath, entry))
72
+ if (!isUnder(base, mirrorAbs)) continue
73
+ // Deepest match wins; an equal-length path cannot occur twice.
74
+ if (mirrorDir && mirrorAbs.length <= mirrorDir.length) continue
75
+ try {
76
+ const meta = JSON.parse(readFileSync(metaPath, 'utf8'))
77
+ if (!meta || !meta.host) continue
78
+ mirrorDir = mirrorAbs
79
+ remotePath = String(meta.remotePath || '')
80
+ machine = {
81
+ host: String(meta.host),
82
+ port: Number(meta.port) || 22,
83
+ username: String(meta.username || ''),
84
+ // The ~/.ssh/config alias this mirror was created through, when the
85
+ // machine was saved as one (issue #38): it keeps the mirror bound to
86
+ // its machine even if that alias's HostName/port later change.
87
+ alias: String(meta.alias || ''),
88
+ }
89
+ } catch { /* unparsable meta → not a usable binding */ }
90
+ }
91
+ }
92
+ return { mirrorDir, remotePath, machine }
93
+ }
94
+
95
+ /**
96
+ * Stable pool key for a remote identity. Two sessions on the same machine
97
+ * produce the same key and therefore share one SSH connection; sessions on
98
+ * different machines never collide.
99
+ * @param {{host: string, port?: number|string, username?: string}} m - the remote identity.
100
+ * @returns {string} the canonical `user@host:port` key.
101
+ */
102
+ export function poolKey(m) {
103
+ return `${(m && m.username) || ''}@${(m && m.host) || ''}:${Number(m && m.port) || 22}`
104
+ }
105
+
106
+ /** Safe path segment for a session id (mirrors DSH's encodeSegment). */
107
+ export function encodeSegmentSafe(id) {
108
+ const s = String(id || '').replace(/[^A-Za-z0-9._-]/g, '_')
109
+ return s || 'session'
110
+ }
111
+
112
+ /** Read the cwd from a session log header (JSONL / .gz / zstd first frame). */
113
+ export function readSessionHeaderCwd(file) {
114
+ try {
115
+ let buf = readFileSync(file)
116
+ if (/\.zstd$/.test(file)) buf = zlib.zstdDecompressSync(buf)
117
+ else if (/\.gz$/.test(file)) buf = zlib.gunzipSync(buf)
118
+ const nl = buf.indexOf(10)
119
+ if (nl < 0) return ''
120
+ const head = JSON.parse(buf.subarray(0, nl).toString('utf8'))
121
+ return typeof head.cwd === 'string' ? head.cwd : ''
122
+ } catch {
123
+ return ''
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Resolve a session's workspace cwd from the live sessions service, then from
129
+ * durable session logs under `$DSH_HOME/sessions`.
130
+ */
131
+ export function lookupSessionCwd(sessionId, { sessions, dshHome } = {}) {
132
+ if (!sessionId) return ''
133
+ try {
134
+ if (sessions && typeof sessions.get === 'function') {
135
+ const s = sessions.get(sessionId)
136
+ const cwd = s && s.header && s.header.cwd
137
+ if (cwd) return String(cwd)
138
+ }
139
+ } catch { /* sessions service unavailable */ }
140
+ if (!dshHome) return ''
141
+ try {
142
+ const sessionsRoot = path.join(dshHome, 'sessions')
143
+ if (!existsSync(sessionsRoot)) return ''
144
+ const targetDir = encodeSegmentSafe(sessionId)
145
+ for (const projDir of readdirSync(sessionsRoot)) {
146
+ const projPath = path.join(sessionsRoot, projDir)
147
+ if (!statSync(projPath, { throwIfNoEntry: false })?.isDirectory?.()) continue
148
+ const sessDir = path.join(projPath, targetDir)
149
+ if (!statSync(sessDir, { throwIfNoEntry: false })?.isDirectory?.()) continue
150
+ const logFile = ['session.jsonl.zstd', 'session.jsonl', 'session.jsonl.gz']
151
+ .map((n) => path.join(sessDir, n)).find((p) => existsSync(p))
152
+ if (!logFile) continue
153
+ const cwd = readSessionHeaderCwd(logFile)
154
+ if (cwd) return cwd
155
+ }
156
+ } catch { /* session log scan failed */ }
157
+ return ''
158
+ }
159
+
160
+ /** Pull sessionId / local from a JSON HTTP request (query string + body). */
161
+ export function requestSessionHint(req, body = {}) {
162
+ let sessionId = ''
163
+ let local = ''
164
+ try {
165
+ const q = new URL(req.url || '', 'http://localhost').searchParams
166
+ // URLSearchParams has already decoded these values. Decoding twice can
167
+ // discard a literal '%' in a mirror path and lose its machine binding.
168
+ sessionId = q.get('sessionId') || ''
169
+ local = q.get('local') || ''
170
+ } catch { /* malformed url */ }
171
+ if (!sessionId && body && body.sessionId) sessionId = String(body.sessionId)
172
+ if (!local && body && body.local) local = String(body.local)
173
+ return { sessionId: String(sessionId || '').trim(), local: String(local || '').trim() }
174
+ }
package/lib/boot.js CHANGED
@@ -1,42 +1,42 @@
1
- // dsh-remote — package entry point (package.json "main" / exports["."]).
2
- //
3
- // A thin shell around the real plugin body (lib/index.js). Its only job is to
4
- // give the startup self-repair somewhere to live: ES module imports HOIST, so
5
- // code placed inside index.js runs strictly after every static import of it
6
- // has already been resolved — there is no room inside index.js to react to a
7
- // missing sibling file. Dynamic import() is the only place such a guard can
8
- // sit, hence this indirection.
9
- //
10
- // What happens here (see lib/self-heal.js for the policy):
11
- // import('./index.js') → on a missing-own-file crash (the half-updated
12
- // 0.8.92 scenario, issue #126) the CURRENT version's tarball is re-laid from
13
- // npm and the import retried once. Healthy installs pay zero overhead: one
14
- // extra dynamic import of the same file the host would have loaded anyway.
15
- //
16
- // ★ TWO INVARIANTS protect this file:
17
- // 1. Stay minimal and self-contained — only import lib/self-heal.js (which
18
- // only imports lib/update.js + node builtins). Anything richer could be
19
- // the file that is missing.
20
- // 2. Re-export index.js's public surface EXACTLY. The cordis loader, the
21
- // settings UI and tests consume these names; test/self-heal-boot.test.js
22
- // pins the key set so index.js cannot gain an export this shell forgets
23
- // to forward.
24
-
25
- import { importWithSelfHeal } from './self-heal.js'
26
-
27
- const { module: main, repaired } = await importWithSelfHeal(() => import('./index.js'))
28
-
29
- if (repaired) {
30
- console.warn('[dsh-remote] startup self-repair succeeded — the incomplete update has been fully re-applied and this boot is healthy')
31
- }
32
-
33
- export const name = main.name
34
- export const DEFAULT_UPDATE_MODE = main.DEFAULT_UPDATE_MODE
35
- export const decodePickStdout = main.decodePickStdout
36
- export const win32FolderPickScript = main.win32FolderPickScript
37
- export const LOCAL_BOOTSTRAP_TOOLS = main.LOCAL_BOOTSTRAP_TOOLS
38
- export const normalizeLocalTools = main.normalizeLocalTools
39
- export const pickWorkspaceNextStep = main.pickWorkspaceNextStep
40
- export const inject = main.inject
41
- export const Config = main.Config
42
- export const apply = main.apply
1
+ // dsh-remote — package entry point (package.json "main" / exports["."]).
2
+ //
3
+ // A thin shell around the real plugin body (lib/index.js). Its only job is to
4
+ // give the startup self-repair somewhere to live: ES module imports HOIST, so
5
+ // code placed inside index.js runs strictly after every static import of it
6
+ // has already been resolved — there is no room inside index.js to react to a
7
+ // missing sibling file. Dynamic import() is the only place such a guard can
8
+ // sit, hence this indirection.
9
+ //
10
+ // What happens here (see lib/self-heal.js for the policy):
11
+ // import('./index.js') → on a missing-own-file crash (the half-updated
12
+ // 0.8.92 scenario, issue #126) the CURRENT version's tarball is re-laid from
13
+ // npm and the import retried once. Healthy installs pay zero overhead: one
14
+ // extra dynamic import of the same file the host would have loaded anyway.
15
+ //
16
+ // ★ TWO INVARIANTS protect this file:
17
+ // 1. Stay minimal and self-contained — only import lib/self-heal.js (which
18
+ // only imports lib/update.js + node builtins). Anything richer could be
19
+ // the file that is missing.
20
+ // 2. Re-export index.js's public surface EXACTLY. The cordis loader, the
21
+ // settings UI and tests consume these names; test/self-heal-boot.test.js
22
+ // pins the key set so index.js cannot gain an export this shell forgets
23
+ // to forward.
24
+
25
+ import { importWithSelfHeal } from './self-heal.js'
26
+
27
+ const { module: main, repaired } = await importWithSelfHeal(() => import('./index.js'))
28
+
29
+ if (repaired) {
30
+ console.warn('[dsh-remote] startup self-repair succeeded — the incomplete update has been fully re-applied and this boot is healthy')
31
+ }
32
+
33
+ export const name = main.name
34
+ export const DEFAULT_UPDATE_MODE = main.DEFAULT_UPDATE_MODE
35
+ export const decodePickStdout = main.decodePickStdout
36
+ export const win32FolderPickScript = main.win32FolderPickScript
37
+ export const LOCAL_BOOTSTRAP_TOOLS = main.LOCAL_BOOTSTRAP_TOOLS
38
+ export const normalizeLocalTools = main.normalizeLocalTools
39
+ export const pickWorkspaceNextStep = main.pickWorkspaceNextStep
40
+ export const inject = main.inject
41
+ export const Config = main.Config
42
+ export const apply = main.apply