@andy.ian/dsh-homing 0.0.0-stage → 2.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 (230) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -2
  3. package/bin/auth.mjs +159 -0
  4. package/bin/backup.mjs +118 -0
  5. package/bin/bootstrap-server.mjs +436 -0
  6. package/bin/entry-link.mjs +69 -0
  7. package/bin/nginx-dsh.template.conf +91 -0
  8. package/bin/nginx-gate.conf +9 -0
  9. package/bin/nginx-ws-map.conf +5 -0
  10. package/bin/renew-cert.mjs +127 -0
  11. package/bin/supervisor.mjs +488 -0
  12. package/bin/watchdog.mjs +163 -0
  13. package/client.js +79 -0
  14. package/cordis.patch.yml +30 -0
  15. package/icon.svg +9 -0
  16. package/index.js +3526 -0
  17. package/lib/csrf.mjs +60 -0
  18. package/lib/devices.mjs +221 -0
  19. package/lib/files-page.mjs +592 -0
  20. package/lib/files.mjs +551 -0
  21. package/lib/git.mjs +154 -0
  22. package/lib/limits.mjs +135 -0
  23. package/lib/pty.mjs +361 -0
  24. package/lib/session.mjs +216 -0
  25. package/lib/shell-page.mjs +430 -0
  26. package/lib/shell.mjs +274 -0
  27. package/lib/term-app.js +318 -0
  28. package/lib/term-page.mjs +117 -0
  29. package/lib/vendor/fonts/JetBrainsMono-Regular.ttf +0 -0
  30. package/lib/vendor/fonts/jetbrains-mono-400.woff2 +0 -0
  31. package/lib/vendor/fonts/jetbrains-mono-700.woff2 +0 -0
  32. package/lib/vendor/node_modules/@xterm/addon-fit/LICENSE +19 -0
  33. package/lib/vendor/node_modules/@xterm/addon-fit/README.md +24 -0
  34. package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js +2 -0
  35. package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js.map +1 -0
  36. package/lib/vendor/node_modules/@xterm/addon-fit/package.json +26 -0
  37. package/lib/vendor/node_modules/@xterm/addon-fit/src/FitAddon.ts +90 -0
  38. package/lib/vendor/node_modules/@xterm/addon-fit/typings/addon-fit.d.ts +55 -0
  39. package/lib/vendor/node_modules/asn1/Jenkinsfile +65 -0
  40. package/lib/vendor/node_modules/asn1/LICENSE +19 -0
  41. package/lib/vendor/node_modules/asn1/README.md +50 -0
  42. package/lib/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
  43. package/lib/vendor/node_modules/asn1/lib/ber/index.js +27 -0
  44. package/lib/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
  45. package/lib/vendor/node_modules/asn1/lib/ber/types.js +36 -0
  46. package/lib/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
  47. package/lib/vendor/node_modules/asn1/lib/index.js +20 -0
  48. package/lib/vendor/node_modules/asn1/package.json +31 -0
  49. package/lib/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
  50. package/lib/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
  51. package/lib/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
  52. package/lib/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
  53. package/lib/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
  54. package/lib/vendor/node_modules/safer-buffer/LICENSE +21 -0
  55. package/lib/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
  56. package/lib/vendor/node_modules/safer-buffer/Readme.md +156 -0
  57. package/lib/vendor/node_modules/safer-buffer/dangerous.js +58 -0
  58. package/lib/vendor/node_modules/safer-buffer/package.json +34 -0
  59. package/lib/vendor/node_modules/safer-buffer/safer.js +77 -0
  60. package/lib/vendor/node_modules/safer-buffer/tests.js +406 -0
  61. package/lib/vendor/node_modules/ssh2/.eslintignore +4 -0
  62. package/lib/vendor/node_modules/ssh2/.eslintrc.js +5 -0
  63. package/lib/vendor/node_modules/ssh2/.github/workflows/ci.yml +110 -0
  64. package/lib/vendor/node_modules/ssh2/.github/workflows/lint.yml +27 -0
  65. package/lib/vendor/node_modules/ssh2/LICENSE +19 -0
  66. package/lib/vendor/node_modules/ssh2/README.md +1529 -0
  67. package/lib/vendor/node_modules/ssh2/SFTP.md +413 -0
  68. package/lib/vendor/node_modules/ssh2/examples/server-chat.js +238 -0
  69. package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +134 -0
  70. package/lib/vendor/node_modules/ssh2/install.js +27 -0
  71. package/lib/vendor/node_modules/ssh2/lib/Channel.js +295 -0
  72. package/lib/vendor/node_modules/ssh2/lib/agent.js +1123 -0
  73. package/lib/vendor/node_modules/ssh2/lib/client.js +2176 -0
  74. package/lib/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
  75. package/lib/vendor/node_modules/ssh2/lib/index.js +44 -0
  76. package/lib/vendor/node_modules/ssh2/lib/keygen.js +582 -0
  77. package/lib/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
  78. package/lib/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
  79. package/lib/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
  80. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
  81. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
  82. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
  83. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
  84. package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
  85. package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
  86. package/lib/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
  87. package/lib/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
  88. package/lib/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
  89. package/lib/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
  90. package/lib/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
  91. package/lib/vendor/node_modules/ssh2/lib/server.js +1380 -0
  92. package/lib/vendor/node_modules/ssh2/lib/utils.js +336 -0
  93. package/lib/vendor/node_modules/ssh2/package.json +49 -0
  94. package/lib/vendor/node_modules/ssh2/test/common.js +316 -0
  95. package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +26 -0
  96. package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +33 -0
  97. package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +52 -0
  98. package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +12 -0
  99. package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +5 -0
  100. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +15 -0
  101. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +26 -0
  102. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +30 -0
  103. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +21 -0
  104. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +1 -0
  105. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +7 -0
  106. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +7 -0
  107. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +22 -0
  108. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +1 -0
  109. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +7 -0
  110. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +7 -0
  111. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +23 -0
  112. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +1 -0
  113. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +7 -0
  114. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +7 -0
  115. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +9 -0
  116. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +1 -0
  117. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +7 -0
  118. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +7 -0
  119. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +10 -0
  120. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +1 -0
  121. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +8 -0
  122. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +7 -0
  123. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +10 -0
  124. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +1 -0
  125. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +8 -0
  126. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +7 -0
  127. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +7 -0
  128. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +1 -0
  129. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +7 -0
  130. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +7 -0
  131. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +27 -0
  132. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +1 -0
  133. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +7 -0
  134. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +7 -0
  135. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +28 -0
  136. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +1 -0
  137. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +8 -0
  138. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +7 -0
  139. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +29 -0
  140. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +1 -0
  141. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +8 -0
  142. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +7 -0
  143. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +12 -0
  144. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +1 -0
  145. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +7 -0
  146. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +7 -0
  147. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +15 -0
  148. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +1 -0
  149. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +8 -0
  150. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +7 -0
  151. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +5 -0
  152. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +1 -0
  153. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +7 -0
  154. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +7 -0
  155. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +8 -0
  156. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +1 -0
  157. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +8 -0
  158. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +7 -0
  159. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +27 -0
  160. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +1 -0
  161. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +7 -0
  162. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +7 -0
  163. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +30 -0
  164. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +1 -0
  165. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +8 -0
  166. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +7 -0
  167. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +54 -0
  168. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +1 -0
  169. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +6 -0
  170. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +6 -0
  171. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +17 -0
  172. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +7 -0
  173. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +26 -0
  174. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +7 -0
  175. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +18 -0
  176. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +7 -0
  177. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +9 -0
  178. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +7 -0
  179. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +10 -0
  180. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +7 -0
  181. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +11 -0
  182. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +7 -0
  183. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +11 -0
  184. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +7 -0
  185. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +8 -0
  186. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +7 -0
  187. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +13 -0
  188. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +7 -0
  189. package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +27 -0
  190. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +12 -0
  191. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +5 -0
  192. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +15 -0
  193. package/lib/vendor/node_modules/ssh2/test/test-exec.js +578 -0
  194. package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +486 -0
  195. package/lib/vendor/node_modules/ssh2/test/test-keygen.js +60 -0
  196. package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +1460 -0
  197. package/lib/vendor/node_modules/ssh2/test/test-openssh.js +261 -0
  198. package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +631 -0
  199. package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +177 -0
  200. package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +138 -0
  201. package/lib/vendor/node_modules/ssh2/test/test-sftp.js +842 -0
  202. package/lib/vendor/node_modules/ssh2/test/test-shell.js +109 -0
  203. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +110 -0
  204. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +171 -0
  205. package/lib/vendor/node_modules/ssh2/test/test-userauth.js +611 -0
  206. package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +25 -0
  207. package/lib/vendor/node_modules/ssh2/test/test.js +20 -0
  208. package/lib/vendor/node_modules/ssh2/util/build_pagent.bat +2 -0
  209. package/lib/vendor/node_modules/ssh2/util/pagent.c +88 -0
  210. package/lib/vendor/node_modules/ssh2/util/pagent.exe +0 -0
  211. package/lib/vendor/node_modules/tweetnacl/AUTHORS.md +27 -0
  212. package/lib/vendor/node_modules/tweetnacl/CHANGELOG.md +283 -0
  213. package/lib/vendor/node_modules/tweetnacl/LICENSE +24 -0
  214. package/lib/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
  215. package/lib/vendor/node_modules/tweetnacl/README.md +494 -0
  216. package/lib/vendor/node_modules/tweetnacl/nacl-fast.js +2391 -0
  217. package/lib/vendor/node_modules/tweetnacl/nacl-fast.min.js +1 -0
  218. package/lib/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
  219. package/lib/vendor/node_modules/tweetnacl/nacl.js +1178 -0
  220. package/lib/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
  221. package/lib/vendor/node_modules/tweetnacl/package.json +56 -0
  222. package/lib/vendor/xterm.css +218 -0
  223. package/lib/vendor/xterm.js +2 -0
  224. package/locale/en.json +6 -0
  225. package/locale/zh.json +6 -0
  226. package/ops/watchdog/dsh-watchdog.ps1 +105 -0
  227. package/ops/watchdog/restart-hidden.vbs +7 -0
  228. package/ops/watchdog/restart-now.ps1 +261 -0
  229. package/ops/watchdog/run-hidden.vbs +4 -0
  230. package/package.json +71 -4
package/lib/files.mjs ADDED
@@ -0,0 +1,551 @@
1
+ /**
2
+ * 文件浏览 / 执行:给 /files 页面用的后端能力。
3
+ *
4
+ * 安全边界(这是能执行命令的页面,必须写清楚):
5
+ * - 所有入口都要求已登录会话(在 index.js 路由层判断,本模块不负责认证);
6
+ * - 路径一律走 resolve() 规范化,页面侧只接受绝对路径,不接受 ".." 逃逸;
7
+ * - 读取有大小上限,二进制文件不渲染;
8
+ * - 执行有超时与输出上限,禁止 shell 拼接(args 单独传,不经过 shell)。
9
+ */
10
+ import fs from 'node:fs'
11
+ import os from 'node:os'
12
+ import path from 'node:path'
13
+ import { spawn } from 'node:child_process'
14
+
15
+ const MAX_READ = 512 * 1024
16
+ const MAX_OUTPUT = 64 * 1024
17
+ const MAX_ENTRIES = 2000
18
+
19
+ export const HOME = os.homedir()
20
+
21
+ /** 把用户输入规范成绝对路径;支持 ~ 与相对路径(相对 HOME)。 */
22
+ export function resolvePath(input, cwd = HOME) {
23
+ const raw = String(input ?? '').trim()
24
+ if (raw === '') return path.resolve(cwd)
25
+ const expanded = raw === '~' ? HOME : raw.startsWith('~/') || raw.startsWith('~\\') ? path.join(HOME, raw.slice(2)) : raw
26
+ return path.resolve(cwd, expanded)
27
+ }
28
+
29
+ const fmtSize = (bytes) => {
30
+ if (bytes < 1024) return `${bytes} B`
31
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
32
+ if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`
33
+ if (bytes < 1024 ** 4) return `${(bytes / 1024 ** 3).toFixed(1)} GB`
34
+ return `${(bytes / 1024 ** 4).toFixed(1)} TB`
35
+ }
36
+
37
+ const fmtTime = (date) => {
38
+ const pad = (n) => String(n).padStart(2, '0')
39
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
40
+ }
41
+
42
+ /**
43
+ * 按「会话标题」反查它所在的工作区目录。
44
+ * 应用里点的是会话行(标题 + 「4天」这类相对时间),不是工作区名,
45
+ * 所以先拿标题去匹配会话记录,再用记录里的 identity.cwd 得到目录。
46
+ */
47
+ export function sessionCwd(dshHome, hint) {
48
+ const squash = (text) => String(text ?? '').replace(/\s+/gu, '');
49
+ const wanted = squash(hint);
50
+ if (wanted === '') return null;
51
+ const dir = path.join(dshHome, 'storages', 'session_projcache', 'sessions');
52
+ let names = [];
53
+ try {
54
+ names = fs.readdirSync(dir);
55
+ } catch {
56
+ return null;
57
+ }
58
+ // 同名会话可能在多个工作区都存在,取修改时间最新的那个
59
+ let best = null;
60
+ for (const name of names) {
61
+ if (!name.endsWith('.json')) continue;
62
+ let mtimeMs = 0;
63
+ try {
64
+ mtimeMs = fs.statSync(path.join(dir, name)).mtimeMs;
65
+ } catch {
66
+ mtimeMs = 0;
67
+ }
68
+ try {
69
+ const data = JSON.parse(fs.readFileSync(path.join(dir, name), 'utf8'));
70
+ const rows = (data && data.record && data.record.rows) || {};
71
+ const title = rows.title && typeof rows.title.val === 'string' ? rows.title.val : '';
72
+ const cwd = (data.record && data.record.identity && data.record.identity.cwd)
73
+ || (rows.workingDirectory && rows.workingDirectory.val)
74
+ || '';
75
+ if (title === '' || typeof cwd !== 'string' || cwd === '') continue;
76
+ if (!wanted.includes(squash(title))) continue;
77
+ if (best === null || mtimeMs > best.mtimeMs
78
+ || (mtimeMs === best.mtimeMs && title.length > best.title.length)) {
79
+ best = { title, cwd, mtimeMs };
80
+ }
81
+ } catch {
82
+ // 坏文件跳过
83
+ }
84
+ }
85
+ return best === null ? null : best.cwd;
86
+ }
87
+
88
+ /** 按 sessionId 查它所属的工作区路径(workspace.json 的 sessionIds 反查)。 */
89
+ export function workspacePathForSessionId(dshHome, sessionId) {
90
+ const id = String(sessionId ?? '').trim();
91
+ if (id === '') return null;
92
+ try {
93
+ const data = JSON.parse(fs.readFileSync(path.join(dshHome, 'storages', 'workspace.json'), 'utf8'));
94
+ const workspaces = (data && data.tables && data.tables.workspaces) || {};
95
+ for (const item of Object.values(workspaces)) {
96
+ if (!item || typeof item.path !== 'string') continue;
97
+ const ids = Array.isArray(item.sessionIds) ? item.sessionIds : [];
98
+ if (ids.includes(id)) return item.path;
99
+ }
100
+ } catch {
101
+ return null;
102
+ }
103
+ return null;
104
+ }
105
+
106
+ /** 按 sessionId 读会话记录里的 cwd(工作区没登记时兜底用)。 */
107
+ export function sessionCwdById(dshHome, sessionId) {
108
+ const id = String(sessionId ?? '').trim();
109
+ if (!/^session-[A-Za-z0-9-]+$/u.test(id)) return null;
110
+ try {
111
+ const file = path.join(dshHome, 'storages', 'session_projcache', 'sessions', id + '.json');
112
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
113
+ const rows = (data && data.record && data.record.rows) || {};
114
+ const cwd = (data.record && data.record.identity && data.record.identity.cwd)
115
+ || (rows.workingDirectory && rows.workingDirectory.val)
116
+ || '';
117
+ return cwd === '' ? null : cwd;
118
+ } catch {
119
+ return null;
120
+ }
121
+ }
122
+
123
+ export function workspacePathForSessionTitle(dshHome, hint) {
124
+ const squash = (text) => String(text ?? '').replace(/\s+/gu, '');
125
+ const wanted = squash(hint);
126
+ if (wanted === '') return null;
127
+
128
+ // 收集所有标题匹配的会话(同名会话可能有多个,甚至有的不属于任何工作区)
129
+ const sessionsDir = path.join(dshHome, 'storages', 'session_projcache', 'sessions');
130
+ let names = [];
131
+ try {
132
+ names = fs.readdirSync(sessionsDir);
133
+ } catch {
134
+ return null;
135
+ }
136
+ const matches = [];
137
+ for (const name of names) {
138
+ if (!name.endsWith('.json')) continue;
139
+ let mtimeMs = 0;
140
+ try {
141
+ mtimeMs = fs.statSync(path.join(sessionsDir, name)).mtimeMs;
142
+ } catch {
143
+ mtimeMs = 0;
144
+ }
145
+ try {
146
+ const data = JSON.parse(fs.readFileSync(path.join(sessionsDir, name), 'utf8'));
147
+ const rows = (data && data.record && data.record.rows) || {};
148
+ const title = rows.title && typeof rows.title.val === 'string' ? rows.title.val : '';
149
+ if (title === '' || !wanted.includes(squash(title))) continue;
150
+ matches.push({ id: name.replace(/\.json$/u, ''), mtimeMs });
151
+ } catch {
152
+ // 坏文件跳过
153
+ }
154
+ }
155
+ if (matches.length === 0) return null;
156
+ matches.sort((a, b) => b.mtimeMs - a.mtimeMs);
157
+
158
+ // 会话 id → 工作区
159
+ let workspaces = {};
160
+ try {
161
+ const data = JSON.parse(fs.readFileSync(path.join(dshHome, 'storages', 'workspace.json'), 'utf8'));
162
+ workspaces = (data && data.tables && data.tables.workspaces) || {};
163
+ } catch {
164
+ return null;
165
+ }
166
+ // 关键:只在"登记在某个工作区里"的会话中挑,避免命中不属于任何工作区的
167
+ // 同名幽灵会话(它们的 cwd 往往指向别处,会把目录打偏)。
168
+ for (const session of matches) {
169
+ for (const item of Object.values(workspaces)) {
170
+ if (!item || typeof item.path !== 'string') continue;
171
+ const ids = Array.isArray(item.sessionIds) ? item.sessionIds : [];
172
+ if (ids.includes(session.id)) return item.path;
173
+ }
174
+ }
175
+ return null;
176
+ }
177
+
178
+ /**
179
+ * 读 DSH 的工作区注册表:
180
+ * list 所有工作区(标题 + 路径),给文件页做快捷入口
181
+ * defaultPath 默认工作区的路径(没配置时为 null)
182
+ * find(名字) 按标题或目录名找路径,找不到返回 null
183
+ */
184
+ export function workspaceIndex(dshHome) {
185
+ const empty = { list: [], defaultPath: null, find: () => null };
186
+ const file = path.join(dshHome, 'storages', 'workspace.json');
187
+ try {
188
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
189
+ const table = (data && data.tables && data.tables.workspaces) || {};
190
+ const list = Object.values(table)
191
+ .filter((item) => item && typeof item.path === 'string')
192
+ .map((item) => ({
193
+ title: typeof item.title === 'string' && item.title !== '' ? item.title : path.basename(item.path),
194
+ path: item.path,
195
+ }));
196
+ const defaultId = data && data.global && data.global.defaultWorkspaceId;
197
+ const defaultPath = defaultId && table[defaultId] && typeof table[defaultId].path === 'string'
198
+ ? table[defaultId].path
199
+ : null;
200
+ const find = (name) => {
201
+ const key = String(name ?? '').trim();
202
+ if (key === '') return null;
203
+ const hit = list.find((item) => item.title === key)
204
+ ?? list.find((item) => path.basename(item.path) === key)
205
+ ?? list.find((item) => item.path === key);
206
+ return hit === undefined ? null : hit.path;
207
+ };
208
+ return { list, defaultPath, find };
209
+ } catch {
210
+ return empty;
211
+ }
212
+ }
213
+
214
+ /** 从 DSH 的工作区注册表里读出「标题 + 路径」,给文件页做快捷入口。 */
215
+ export function listWorkspaces(dshHome) {
216
+ const file = path.join(dshHome, 'storages', 'workspace.json');
217
+ try {
218
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
219
+ const table = (data && data.tables && data.tables.workspaces) || {};
220
+ return Object.values(table)
221
+ .filter((item) => item && typeof item.path === 'string')
222
+ .map((item) => ({
223
+ title: typeof item.title === 'string' && item.title !== '' ? item.title : path.basename(item.path),
224
+ path: item.path,
225
+ }));
226
+ } catch {
227
+ return [];
228
+ }
229
+ }
230
+
231
+ /** 列目录。不可读时返回 error 而不是抛错,页面好展示。 */
232
+ export function listDirectory(absPath) {
233
+ const target = resolvePath(absPath)
234
+ let stat
235
+ try {
236
+ stat = fs.statSync(target)
237
+ } catch (error) {
238
+ return { path: target, error: `打不开:${error.code ?? error.message}`, entries: [] }
239
+ }
240
+ if (!stat.isDirectory()) return { path: target, error: '这不是目录', entries: [] }
241
+
242
+ let names = []
243
+ try {
244
+ names = fs.readdirSync(target)
245
+ } catch (error) {
246
+ return { path: target, error: `读不了目录:${error.code ?? error.message}`, entries: [] }
247
+ }
248
+
249
+ const entries = []
250
+ for (const name of names.slice(0, MAX_ENTRIES)) {
251
+ const full = path.join(target, name)
252
+ try {
253
+ const st = fs.statSync(full)
254
+ entries.push({
255
+ name,
256
+ path: full,
257
+ dir: st.isDirectory(),
258
+ size: st.size,
259
+ sizeText: st.isDirectory() ? '' : fmtSize(st.size),
260
+ mtime: fmtTime(st.mtime),
261
+ mtimeMs: st.mtimeMs,
262
+ runnable: !st.isDirectory() && runnableKind(full) !== null,
263
+ })
264
+ } catch {
265
+ entries.push({ name, path: full, dir: false, size: 0, sizeText: '?', mtime: '', mtimeMs: 0, runnable: false })
266
+ }
267
+ }
268
+ // 目录在前,其余按名字排序(不区分大小写)
269
+ entries.sort((a, b) => (a.dir === b.dir ? a.name.localeCompare(b.name, 'zh') : a.dir ? -1 : 1))
270
+
271
+ const parent = path.dirname(target)
272
+ return {
273
+ path: target,
274
+ parent: parent === target ? null : parent,
275
+ entries,
276
+ truncated: names.length > MAX_ENTRIES ? names.length : 0,
277
+ }
278
+ }
279
+
280
+ /** 判断这个扩展名能不能直接"运行"。 */
281
+ export function runnableKind(file) {
282
+ const ext = path.extname(file).toLowerCase()
283
+ if (process.platform === 'win32') {
284
+ if (ext === '.exe' || ext === '.com') return 'exe'
285
+ if (ext === '.bat' || ext === '.cmd') return 'bat'
286
+ if (ext === '.ps1') return 'ps1'
287
+ }
288
+ if (ext === '.sh') return 'sh'
289
+ return null
290
+ }
291
+
292
+ /** 读文本文件;二进制或过大时不返回正文。 */
293
+ export function readTextFile(absPath, limit = MAX_READ) {
294
+ const target = resolvePath(absPath)
295
+ let st
296
+ try {
297
+ st = fs.statSync(target)
298
+ } catch (error) {
299
+ return { path: target, error: `打不开:${error.code ?? error.message}` }
300
+ }
301
+ if (st.isDirectory()) return { path: target, error: '这是目录' }
302
+ if (st.size > limit) {
303
+ return { path: target, sizeText: fmtSize(st.size), error: `文件太大(${fmtSize(st.size)}),超过 ${fmtSize(limit)} 不预览` }
304
+ }
305
+ const buffer = fs.readFileSync(target)
306
+ if (buffer.includes(0)) {
307
+ return { path: target, sizeText: fmtSize(st.size), binary: true, error: '二进制文件,不预览' }
308
+ }
309
+ return {
310
+ path: target,
311
+ sizeText: fmtSize(st.size),
312
+ mtime: fmtTime(st.mtime),
313
+ text: buffer.toString('utf8'),
314
+ }
315
+ }
316
+
317
+ /**
318
+ * 参数分词:支持用引号包住带空格的参数。
319
+ * '"feat: 新功能" -DryRun' → ['feat: 新功能', '-DryRun']
320
+ * 不做 shell 展开(不认 $VAR、通配符),参数原样交给被调用程序。
321
+ */
322
+ export function splitArgs(input) {
323
+ const out = [];
324
+ let current = '';
325
+ let quote = null;
326
+ for (const char of String(input ?? '')) {
327
+ if (quote !== null) {
328
+ if (char === quote) { quote = null; continue; }
329
+ current += char;
330
+ continue;
331
+ }
332
+ if (char === '"' || char === "'") { quote = char; continue; }
333
+ if (/\s/u.test(char)) {
334
+ if (current !== '') { out.push(current); current = ''; }
335
+ continue;
336
+ }
337
+ current += char;
338
+ }
339
+ if (current !== '') out.push(current);
340
+ return out;
341
+ }
342
+
343
+ /** 保存文本文件。保留原文件权限;写入前做大小上限检查。 */
344
+ export function writeTextFile(absPath, text, limit = MAX_READ) {
345
+ const target = resolvePath(absPath);
346
+ let stat = null;
347
+ try {
348
+ stat = fs.statSync(target);
349
+ } catch {
350
+ stat = null;
351
+ }
352
+ if (stat !== null && stat.isDirectory()) return { ok: false, error: '这是目录' };
353
+ const content = String(text ?? '');
354
+ if (Buffer.byteLength(content, 'utf8') > limit) return { ok: false, error: '内容太大(超过 512KB)' };
355
+ try {
356
+ fs.writeFileSync(target, content, 'utf8');
357
+ return { ok: true, size: Buffer.byteLength(content, 'utf8') };
358
+ } catch (error) {
359
+ return { ok: false, error: '写入失败:' + (error.code ?? error.message) };
360
+ }
361
+ }
362
+
363
+ /** 按类型执行文件,收集输出。不经过 shell 拼接(bat/ps1/sh 由解释器接手)。 */
364
+ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
365
+ const target = resolvePath(absPath);
366
+ const kind = runnableKind(target);
367
+ if (kind === null) return Promise.resolve({ ok: false, error: '这个文件类型不支持直接运行' });
368
+ if (!fs.existsSync(target)) return Promise.resolve({ ok: false, error: '文件不存在' });
369
+
370
+ const argList = splitArgs(args);
371
+
372
+ // 中文 Windows 的 PowerShell / cmd 默认用 GBK 输出。为了既强制 UTF-8、又保留 -File
373
+ // 的参数绑定(switch 参数不能被转成字符串),用一个包装脚本:先设 OutputEncoding,
374
+ // 再用 & 调用目标并透传参数。不能塞进 cmd /c 一行命令——路径的引号会被 cmd 吃掉。
375
+ let command = target;
376
+ let argv = argList;
377
+ if (kind === 'ps1') {
378
+ const wrapper = path.join(os.tmpdir(), 'dsh-homing-run-ps1.ps1');
379
+ fs.writeFileSync(wrapper, [
380
+ '[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
381
+ '$target = $args[0]',
382
+ '$rest = @($args | Select-Object -Skip 1)',
383
+ '& $target @rest',
384
+ 'exit $LASTEXITCODE',
385
+ ].join('\r\n'), 'utf8');
386
+ command = 'powershell.exe';
387
+ argv = ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', wrapper, target, ...argList];
388
+ } else if (kind === 'bat') {
389
+ command = 'cmd.exe';
390
+ const tail = argList.map((item) => ' "' + item.replace(/"/gu, '""') + '"').join('');
391
+ argv = ['/d', '/c', 'chcp 65001>nul & call "' + target + '"' + tail];
392
+ } else if (kind === 'sh') {
393
+ command = 'bash';
394
+ argv = [target, ...argList];
395
+ }
396
+
397
+ return new Promise((resolve) => {
398
+ let settled = false;
399
+ const finish = (value) => {
400
+ if (settled) return;
401
+ settled = true;
402
+ resolve(value);
403
+ };
404
+
405
+ let child;
406
+ try {
407
+ child = spawn(command, argv, {
408
+ cwd: cwd ?? path.dirname(target),
409
+ windowsHide: true,
410
+ // bat 走 cmd /c 一行命令,引号由我们自己拼好;不能让 Node 再转义一遍
411
+ windowsVerbatimArguments: kind === 'bat',
412
+ stdio: ['ignore', 'pipe', 'pipe'],
413
+ });
414
+ } catch (error) {
415
+ finish({ ok: false, error: '启动失败:' + error.message });
416
+ return;
417
+ }
418
+
419
+ let truncated = false;
420
+ const chunks = [];
421
+ let total = 0;
422
+ const collect = (chunk) => {
423
+ if (total >= MAX_OUTPUT) { truncated = true; return; }
424
+ chunks.push(chunk);
425
+ total += chunk.length;
426
+ if (total > MAX_OUTPUT) truncated = true;
427
+ };
428
+ child.stdout.on('data', collect);
429
+ child.stderr.on('data', collect);
430
+
431
+ // 已强制子进程输出 UTF-8:优先按 UTF-8 解,只有出现非法序列才退回 GBK
432
+ const decode = () => {
433
+ const buffer = Buffer.concat(chunks).subarray(0, MAX_OUTPUT);
434
+ const utf8 = buffer.toString('utf8');
435
+ if (!utf8.includes('\uFFFD')) return utf8;
436
+ try {
437
+ return new TextDecoder('gbk').decode(buffer);
438
+ } catch {
439
+ return utf8;
440
+ }
441
+ };
442
+
443
+ const timer = setTimeout(() => {
444
+ try {
445
+ if (process.platform === 'win32' && child.pid) {
446
+ spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
447
+ } else {
448
+ child.kill('SIGKILL');
449
+ }
450
+ } catch { /* 已退出 */ }
451
+ finish({ ok: true, code: null, output: decode(), truncated, timedOut: true });
452
+ }, timeoutMs);
453
+
454
+ child.on('error', (error) => {
455
+ clearTimeout(timer);
456
+ finish({ ok: false, error: '启动失败:' + error.message });
457
+ });
458
+ child.on('close', (code) => {
459
+ clearTimeout(timer);
460
+ finish({ ok: true, code, output: decode(), truncated, timedOut: false });
461
+ });
462
+ });
463
+ }
464
+
465
+ export { fmtSize, fmtTime }
466
+
467
+ /**
468
+ * 重命名同一目录下的文件/目录。
469
+ * 只接受纯名字(不含路径分隔符),目标已存在则拒绝,绝不跨目录移动。
470
+ */
471
+ export function renameEntry(absPath, newName) {
472
+ const bad = /[\\/:*?"<>|\u0000-\u001f]/u;
473
+ const name = String(newName ?? '').trim();
474
+ if (!name) return { ok: false, error: '名字不能为空' };
475
+ if (name === '.' || name === '..') return { ok: false, error: '非法名字' };
476
+ if (bad.test(name)) return { ok: false, error: '名字里不能包含 \\ / : * ? " < > |' };
477
+ const dir = path.dirname(absPath);
478
+ const target = path.join(dir, name);
479
+ if (path.resolve(target) === path.resolve(absPath)) return { ok: true, path: absPath };
480
+ if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
481
+ fs.renameSync(absPath, target);
482
+ return { ok: true, path: target };
483
+ }
484
+
485
+ /** 删除文件或目录(目录递归)。调用方必须已做二次确认。 */
486
+ export function deleteEntry(absPath) {
487
+ const target = path.resolve(String(absPath ?? ''));
488
+ if (!target || !fs.existsSync(target)) return { ok: false, error: '不存在' };
489
+ const stat = fs.statSync(target);
490
+ if (stat.isDirectory()) fs.rmSync(target, { recursive: true, force: true });
491
+ else fs.unlinkSync(target);
492
+ return { ok: true };
493
+ }
494
+
495
+ /** 把 src 复制进 destDir;同名则自动加 " - 副本"/" - 副本2" 后缀,绝不覆盖。 */
496
+ export function copyInto(srcPath, destDir) {
497
+ const src = path.resolve(String(srcPath ?? ''));
498
+ const dir = path.resolve(String(destDir ?? ''));
499
+ if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
500
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
501
+ const base = path.basename(src);
502
+ const ext = path.extname(base);
503
+ const stem = ext ? base.slice(0, -ext.length) : base;
504
+ let target = path.join(dir, base);
505
+ let n = 1;
506
+ while (fs.existsSync(target)) {
507
+ const name = ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`;
508
+ target = path.join(dir, name);
509
+ n += 1;
510
+ if (n > 100) return { ok: false, error: '同名文件过多' };
511
+ }
512
+ fs.cpSync(src, target, { recursive: true, errorOnExist: false, force: false });
513
+ return { ok: true, path: target };
514
+ }
515
+
516
+ const BAD_NAME = /[\\/:*?"<>|\u0000-\u001f]/u;
517
+
518
+ /** 在 dir 下新建文件或目录;名字非法或已存在则拒绝。 */
519
+ export function createEntry(dirPath, name, isDir) {
520
+ const dir = path.resolve(String(dirPath ?? ''));
521
+ const clean = String(name ?? '').trim();
522
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
523
+ if (!clean) return { ok: false, error: '名字不能为空' };
524
+ if (clean === '.' || clean === '..' || BAD_NAME.test(clean)) return { ok: false, error: '名字不合法' };
525
+ const target = path.join(dir, clean);
526
+ if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
527
+ if (isDir) fs.mkdirSync(target);
528
+ else fs.writeFileSync(target, '');
529
+ return { ok: true, path: target };
530
+ }
531
+
532
+ /** 把 src 移动进 destDir;同名自动改名,绝不覆盖。 */
533
+ export function moveInto(srcPath, destDir) {
534
+ const src = path.resolve(String(srcPath ?? ''));
535
+ const dir = path.resolve(String(destDir ?? ''));
536
+ if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
537
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
538
+ if (path.dirname(src) === dir) return { ok: false, error: '已经在目标目录里' };
539
+ const base = path.basename(src);
540
+ const ext = path.extname(base);
541
+ const stem = ext ? base.slice(0, -ext.length) : base;
542
+ let target = path.join(dir, base);
543
+ let n = 1;
544
+ while (fs.existsSync(target)) {
545
+ target = path.join(dir, ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`);
546
+ n += 1;
547
+ if (n > 100) return { ok: false, error: '同名文件过多' };
548
+ }
549
+ fs.renameSync(src, target);
550
+ return { ok: true, path: target };
551
+ }