@andy.ian/dsh-homing 0.0.0-stage → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) 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/watchdog.mjs +158 -0
  12. package/cordis.patch.yml +30 -0
  13. package/icon.svg +9 -0
  14. package/index.js +3339 -0
  15. package/lib/csrf.mjs +60 -0
  16. package/lib/devices.mjs +221 -0
  17. package/lib/files-page.mjs +391 -0
  18. package/lib/files.mjs +464 -0
  19. package/lib/git.mjs +154 -0
  20. package/lib/limits.mjs +135 -0
  21. package/lib/pty.mjs +354 -0
  22. package/lib/session.mjs +216 -0
  23. package/lib/shell-page.mjs +430 -0
  24. package/lib/shell.mjs +274 -0
  25. package/lib/term-app.js +318 -0
  26. package/lib/term-page.mjs +117 -0
  27. package/lib/vendor/fonts/JetBrainsMono-Regular.ttf +0 -0
  28. package/lib/vendor/fonts/jetbrains-mono-400.woff2 +0 -0
  29. package/lib/vendor/fonts/jetbrains-mono-700.woff2 +0 -0
  30. package/lib/vendor/node_modules/@xterm/addon-fit/LICENSE +19 -0
  31. package/lib/vendor/node_modules/@xterm/addon-fit/README.md +24 -0
  32. package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js +2 -0
  33. package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js.map +1 -0
  34. package/lib/vendor/node_modules/@xterm/addon-fit/package.json +26 -0
  35. package/lib/vendor/node_modules/@xterm/addon-fit/src/FitAddon.ts +90 -0
  36. package/lib/vendor/node_modules/@xterm/addon-fit/typings/addon-fit.d.ts +55 -0
  37. package/lib/vendor/node_modules/asn1/Jenkinsfile +65 -0
  38. package/lib/vendor/node_modules/asn1/LICENSE +19 -0
  39. package/lib/vendor/node_modules/asn1/README.md +50 -0
  40. package/lib/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
  41. package/lib/vendor/node_modules/asn1/lib/ber/index.js +27 -0
  42. package/lib/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
  43. package/lib/vendor/node_modules/asn1/lib/ber/types.js +36 -0
  44. package/lib/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
  45. package/lib/vendor/node_modules/asn1/lib/index.js +20 -0
  46. package/lib/vendor/node_modules/asn1/package.json +31 -0
  47. package/lib/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
  48. package/lib/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
  49. package/lib/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
  50. package/lib/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
  51. package/lib/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
  52. package/lib/vendor/node_modules/safer-buffer/LICENSE +21 -0
  53. package/lib/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
  54. package/lib/vendor/node_modules/safer-buffer/Readme.md +156 -0
  55. package/lib/vendor/node_modules/safer-buffer/dangerous.js +58 -0
  56. package/lib/vendor/node_modules/safer-buffer/package.json +34 -0
  57. package/lib/vendor/node_modules/safer-buffer/safer.js +77 -0
  58. package/lib/vendor/node_modules/safer-buffer/tests.js +406 -0
  59. package/lib/vendor/node_modules/ssh2/.eslintignore +4 -0
  60. package/lib/vendor/node_modules/ssh2/.eslintrc.js +5 -0
  61. package/lib/vendor/node_modules/ssh2/.github/workflows/ci.yml +110 -0
  62. package/lib/vendor/node_modules/ssh2/.github/workflows/lint.yml +27 -0
  63. package/lib/vendor/node_modules/ssh2/LICENSE +19 -0
  64. package/lib/vendor/node_modules/ssh2/README.md +1529 -0
  65. package/lib/vendor/node_modules/ssh2/SFTP.md +413 -0
  66. package/lib/vendor/node_modules/ssh2/examples/server-chat.js +238 -0
  67. package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +134 -0
  68. package/lib/vendor/node_modules/ssh2/install.js +27 -0
  69. package/lib/vendor/node_modules/ssh2/lib/Channel.js +295 -0
  70. package/lib/vendor/node_modules/ssh2/lib/agent.js +1123 -0
  71. package/lib/vendor/node_modules/ssh2/lib/client.js +2176 -0
  72. package/lib/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
  73. package/lib/vendor/node_modules/ssh2/lib/index.js +44 -0
  74. package/lib/vendor/node_modules/ssh2/lib/keygen.js +582 -0
  75. package/lib/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
  76. package/lib/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
  77. package/lib/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
  78. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
  79. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
  80. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
  81. package/lib/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
  82. package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
  83. package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
  84. package/lib/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
  85. package/lib/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
  86. package/lib/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
  87. package/lib/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
  88. package/lib/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
  89. package/lib/vendor/node_modules/ssh2/lib/server.js +1380 -0
  90. package/lib/vendor/node_modules/ssh2/lib/utils.js +336 -0
  91. package/lib/vendor/node_modules/ssh2/package.json +49 -0
  92. package/lib/vendor/node_modules/ssh2/test/common.js +316 -0
  93. package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +26 -0
  94. package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +33 -0
  95. package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +52 -0
  96. package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +12 -0
  97. package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +5 -0
  98. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +15 -0
  99. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +26 -0
  100. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +30 -0
  101. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +21 -0
  102. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +1 -0
  103. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +7 -0
  104. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +7 -0
  105. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +22 -0
  106. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +1 -0
  107. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +7 -0
  108. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +7 -0
  109. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +23 -0
  110. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +1 -0
  111. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +7 -0
  112. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +7 -0
  113. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +9 -0
  114. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +1 -0
  115. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +7 -0
  116. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +7 -0
  117. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +10 -0
  118. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +1 -0
  119. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +8 -0
  120. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +7 -0
  121. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +10 -0
  122. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +1 -0
  123. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +8 -0
  124. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +7 -0
  125. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +7 -0
  126. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +1 -0
  127. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +7 -0
  128. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +7 -0
  129. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +27 -0
  130. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +1 -0
  131. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +7 -0
  132. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +7 -0
  133. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +28 -0
  134. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +1 -0
  135. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +8 -0
  136. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +7 -0
  137. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +29 -0
  138. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +1 -0
  139. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +8 -0
  140. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +7 -0
  141. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +12 -0
  142. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +1 -0
  143. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +7 -0
  144. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +7 -0
  145. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +15 -0
  146. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +1 -0
  147. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +8 -0
  148. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +7 -0
  149. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +5 -0
  150. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +1 -0
  151. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +7 -0
  152. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +7 -0
  153. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +8 -0
  154. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +1 -0
  155. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +8 -0
  156. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +7 -0
  157. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +27 -0
  158. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +1 -0
  159. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +7 -0
  160. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +7 -0
  161. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +30 -0
  162. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +1 -0
  163. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +8 -0
  164. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +7 -0
  165. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +54 -0
  166. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +1 -0
  167. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +6 -0
  168. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +6 -0
  169. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +17 -0
  170. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +7 -0
  171. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +26 -0
  172. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +7 -0
  173. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +18 -0
  174. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +7 -0
  175. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +9 -0
  176. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +7 -0
  177. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +10 -0
  178. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +7 -0
  179. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +11 -0
  180. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +7 -0
  181. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +11 -0
  182. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +7 -0
  183. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +8 -0
  184. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +7 -0
  185. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +13 -0
  186. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +7 -0
  187. package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +27 -0
  188. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +12 -0
  189. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +5 -0
  190. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +15 -0
  191. package/lib/vendor/node_modules/ssh2/test/test-exec.js +578 -0
  192. package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +486 -0
  193. package/lib/vendor/node_modules/ssh2/test/test-keygen.js +60 -0
  194. package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +1460 -0
  195. package/lib/vendor/node_modules/ssh2/test/test-openssh.js +261 -0
  196. package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +631 -0
  197. package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +177 -0
  198. package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +138 -0
  199. package/lib/vendor/node_modules/ssh2/test/test-sftp.js +842 -0
  200. package/lib/vendor/node_modules/ssh2/test/test-shell.js +109 -0
  201. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +110 -0
  202. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +171 -0
  203. package/lib/vendor/node_modules/ssh2/test/test-userauth.js +611 -0
  204. package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +25 -0
  205. package/lib/vendor/node_modules/ssh2/test/test.js +20 -0
  206. package/lib/vendor/node_modules/ssh2/util/build_pagent.bat +2 -0
  207. package/lib/vendor/node_modules/ssh2/util/pagent.c +88 -0
  208. package/lib/vendor/node_modules/ssh2/util/pagent.exe +0 -0
  209. package/lib/vendor/node_modules/tweetnacl/AUTHORS.md +27 -0
  210. package/lib/vendor/node_modules/tweetnacl/CHANGELOG.md +283 -0
  211. package/lib/vendor/node_modules/tweetnacl/LICENSE +24 -0
  212. package/lib/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
  213. package/lib/vendor/node_modules/tweetnacl/README.md +494 -0
  214. package/lib/vendor/node_modules/tweetnacl/nacl-fast.js +2391 -0
  215. package/lib/vendor/node_modules/tweetnacl/nacl-fast.min.js +1 -0
  216. package/lib/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
  217. package/lib/vendor/node_modules/tweetnacl/nacl.js +1178 -0
  218. package/lib/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
  219. package/lib/vendor/node_modules/tweetnacl/package.json +56 -0
  220. package/lib/vendor/xterm.css +218 -0
  221. package/lib/vendor/xterm.js +2 -0
  222. package/locale/en.json +6 -0
  223. package/locale/zh.json +6 -0
  224. package/ops/watchdog/dsh-watchdog.ps1 +105 -0
  225. package/ops/watchdog/restart-now.ps1 +247 -0
  226. package/package.json +69 -4
package/lib/files.mjs ADDED
@@ -0,0 +1,464 @@
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
+ return `${(bytes / 1024 / 1024 / 1024).toFixed(1)} GB`
34
+ }
35
+
36
+ const fmtTime = (date) => {
37
+ const pad = (n) => String(n).padStart(2, '0')
38
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
39
+ }
40
+
41
+ /**
42
+ * 按「会话标题」反查它所在的工作区目录。
43
+ * 应用里点的是会话行(标题 + 「4天」这类相对时间),不是工作区名,
44
+ * 所以先拿标题去匹配会话记录,再用记录里的 identity.cwd 得到目录。
45
+ */
46
+ export function sessionCwd(dshHome, hint) {
47
+ const squash = (text) => String(text ?? '').replace(/\s+/gu, '');
48
+ const wanted = squash(hint);
49
+ if (wanted === '') return null;
50
+ const dir = path.join(dshHome, 'storages', 'session_projcache', 'sessions');
51
+ let names = [];
52
+ try {
53
+ names = fs.readdirSync(dir);
54
+ } catch {
55
+ return null;
56
+ }
57
+ // 同名会话可能在多个工作区都存在,取修改时间最新的那个
58
+ let best = null;
59
+ for (const name of names) {
60
+ if (!name.endsWith('.json')) continue;
61
+ let mtimeMs = 0;
62
+ try {
63
+ mtimeMs = fs.statSync(path.join(dir, name)).mtimeMs;
64
+ } catch {
65
+ mtimeMs = 0;
66
+ }
67
+ try {
68
+ const data = JSON.parse(fs.readFileSync(path.join(dir, name), 'utf8'));
69
+ const rows = (data && data.record && data.record.rows) || {};
70
+ const title = rows.title && typeof rows.title.val === 'string' ? rows.title.val : '';
71
+ const cwd = (data.record && data.record.identity && data.record.identity.cwd)
72
+ || (rows.workingDirectory && rows.workingDirectory.val)
73
+ || '';
74
+ if (title === '' || typeof cwd !== 'string' || cwd === '') continue;
75
+ if (!wanted.includes(squash(title))) continue;
76
+ if (best === null || mtimeMs > best.mtimeMs
77
+ || (mtimeMs === best.mtimeMs && title.length > best.title.length)) {
78
+ best = { title, cwd, mtimeMs };
79
+ }
80
+ } catch {
81
+ // 坏文件跳过
82
+ }
83
+ }
84
+ return best === null ? null : best.cwd;
85
+ }
86
+
87
+ /** 按 sessionId 查它所属的工作区路径(workspace.json 的 sessionIds 反查)。 */
88
+ export function workspacePathForSessionId(dshHome, sessionId) {
89
+ const id = String(sessionId ?? '').trim();
90
+ if (id === '') return null;
91
+ try {
92
+ const data = JSON.parse(fs.readFileSync(path.join(dshHome, 'storages', 'workspace.json'), 'utf8'));
93
+ const workspaces = (data && data.tables && data.tables.workspaces) || {};
94
+ for (const item of Object.values(workspaces)) {
95
+ if (!item || typeof item.path !== 'string') continue;
96
+ const ids = Array.isArray(item.sessionIds) ? item.sessionIds : [];
97
+ if (ids.includes(id)) return item.path;
98
+ }
99
+ } catch {
100
+ return null;
101
+ }
102
+ return null;
103
+ }
104
+
105
+ /** 按 sessionId 读会话记录里的 cwd(工作区没登记时兜底用)。 */
106
+ export function sessionCwdById(dshHome, sessionId) {
107
+ const id = String(sessionId ?? '').trim();
108
+ if (!/^session-[A-Za-z0-9-]+$/u.test(id)) return null;
109
+ try {
110
+ const file = path.join(dshHome, 'storages', 'session_projcache', 'sessions', id + '.json');
111
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
112
+ const rows = (data && data.record && data.record.rows) || {};
113
+ const cwd = (data.record && data.record.identity && data.record.identity.cwd)
114
+ || (rows.workingDirectory && rows.workingDirectory.val)
115
+ || '';
116
+ return cwd === '' ? null : cwd;
117
+ } catch {
118
+ return null;
119
+ }
120
+ }
121
+
122
+ export function workspacePathForSessionTitle(dshHome, hint) {
123
+ const squash = (text) => String(text ?? '').replace(/\s+/gu, '');
124
+ const wanted = squash(hint);
125
+ if (wanted === '') return null;
126
+
127
+ // 收集所有标题匹配的会话(同名会话可能有多个,甚至有的不属于任何工作区)
128
+ const sessionsDir = path.join(dshHome, 'storages', 'session_projcache', 'sessions');
129
+ let names = [];
130
+ try {
131
+ names = fs.readdirSync(sessionsDir);
132
+ } catch {
133
+ return null;
134
+ }
135
+ const matches = [];
136
+ for (const name of names) {
137
+ if (!name.endsWith('.json')) continue;
138
+ let mtimeMs = 0;
139
+ try {
140
+ mtimeMs = fs.statSync(path.join(sessionsDir, name)).mtimeMs;
141
+ } catch {
142
+ mtimeMs = 0;
143
+ }
144
+ try {
145
+ const data = JSON.parse(fs.readFileSync(path.join(sessionsDir, name), 'utf8'));
146
+ const rows = (data && data.record && data.record.rows) || {};
147
+ const title = rows.title && typeof rows.title.val === 'string' ? rows.title.val : '';
148
+ if (title === '' || !wanted.includes(squash(title))) continue;
149
+ matches.push({ id: name.replace(/\.json$/u, ''), mtimeMs });
150
+ } catch {
151
+ // 坏文件跳过
152
+ }
153
+ }
154
+ if (matches.length === 0) return null;
155
+ matches.sort((a, b) => b.mtimeMs - a.mtimeMs);
156
+
157
+ // 会话 id → 工作区
158
+ let workspaces = {};
159
+ try {
160
+ const data = JSON.parse(fs.readFileSync(path.join(dshHome, 'storages', 'workspace.json'), 'utf8'));
161
+ workspaces = (data && data.tables && data.tables.workspaces) || {};
162
+ } catch {
163
+ return null;
164
+ }
165
+ // 关键:只在"登记在某个工作区里"的会话中挑,避免命中不属于任何工作区的
166
+ // 同名幽灵会话(它们的 cwd 往往指向别处,会把目录打偏)。
167
+ for (const session of matches) {
168
+ for (const item of Object.values(workspaces)) {
169
+ if (!item || typeof item.path !== 'string') continue;
170
+ const ids = Array.isArray(item.sessionIds) ? item.sessionIds : [];
171
+ if (ids.includes(session.id)) return item.path;
172
+ }
173
+ }
174
+ return null;
175
+ }
176
+
177
+ /**
178
+ * 读 DSH 的工作区注册表:
179
+ * list 所有工作区(标题 + 路径),给文件页做快捷入口
180
+ * defaultPath 默认工作区的路径(没配置时为 null)
181
+ * find(名字) 按标题或目录名找路径,找不到返回 null
182
+ */
183
+ export function workspaceIndex(dshHome) {
184
+ const empty = { list: [], defaultPath: null, find: () => null };
185
+ const file = path.join(dshHome, 'storages', 'workspace.json');
186
+ try {
187
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
188
+ const table = (data && data.tables && data.tables.workspaces) || {};
189
+ const list = Object.values(table)
190
+ .filter((item) => item && typeof item.path === 'string')
191
+ .map((item) => ({
192
+ title: typeof item.title === 'string' && item.title !== '' ? item.title : path.basename(item.path),
193
+ path: item.path,
194
+ }));
195
+ const defaultId = data && data.global && data.global.defaultWorkspaceId;
196
+ const defaultPath = defaultId && table[defaultId] && typeof table[defaultId].path === 'string'
197
+ ? table[defaultId].path
198
+ : null;
199
+ const find = (name) => {
200
+ const key = String(name ?? '').trim();
201
+ if (key === '') return null;
202
+ const hit = list.find((item) => item.title === key)
203
+ ?? list.find((item) => path.basename(item.path) === key)
204
+ ?? list.find((item) => item.path === key);
205
+ return hit === undefined ? null : hit.path;
206
+ };
207
+ return { list, defaultPath, find };
208
+ } catch {
209
+ return empty;
210
+ }
211
+ }
212
+
213
+ /** 从 DSH 的工作区注册表里读出「标题 + 路径」,给文件页做快捷入口。 */
214
+ export function listWorkspaces(dshHome) {
215
+ const file = path.join(dshHome, 'storages', 'workspace.json');
216
+ try {
217
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'));
218
+ const table = (data && data.tables && data.tables.workspaces) || {};
219
+ return Object.values(table)
220
+ .filter((item) => item && typeof item.path === 'string')
221
+ .map((item) => ({
222
+ title: typeof item.title === 'string' && item.title !== '' ? item.title : path.basename(item.path),
223
+ path: item.path,
224
+ }));
225
+ } catch {
226
+ return [];
227
+ }
228
+ }
229
+
230
+ /** 列目录。不可读时返回 error 而不是抛错,页面好展示。 */
231
+ export function listDirectory(absPath) {
232
+ const target = resolvePath(absPath)
233
+ let stat
234
+ try {
235
+ stat = fs.statSync(target)
236
+ } catch (error) {
237
+ return { path: target, error: `打不开:${error.code ?? error.message}`, entries: [] }
238
+ }
239
+ if (!stat.isDirectory()) return { path: target, error: '这不是目录', entries: [] }
240
+
241
+ let names = []
242
+ try {
243
+ names = fs.readdirSync(target)
244
+ } catch (error) {
245
+ return { path: target, error: `读不了目录:${error.code ?? error.message}`, entries: [] }
246
+ }
247
+
248
+ const entries = []
249
+ for (const name of names.slice(0, MAX_ENTRIES)) {
250
+ const full = path.join(target, name)
251
+ try {
252
+ const st = fs.statSync(full)
253
+ entries.push({
254
+ name,
255
+ path: full,
256
+ dir: st.isDirectory(),
257
+ size: st.size,
258
+ sizeText: st.isDirectory() ? '' : fmtSize(st.size),
259
+ mtime: fmtTime(st.mtime),
260
+ mtimeMs: st.mtimeMs,
261
+ runnable: !st.isDirectory() && runnableKind(full) !== null,
262
+ })
263
+ } catch {
264
+ entries.push({ name, path: full, dir: false, size: 0, sizeText: '?', mtime: '', mtimeMs: 0, runnable: false })
265
+ }
266
+ }
267
+ // 目录在前,其余按名字排序(不区分大小写)
268
+ entries.sort((a, b) => (a.dir === b.dir ? a.name.localeCompare(b.name, 'zh') : a.dir ? -1 : 1))
269
+
270
+ const parent = path.dirname(target)
271
+ return {
272
+ path: target,
273
+ parent: parent === target ? null : parent,
274
+ entries,
275
+ truncated: names.length > MAX_ENTRIES ? names.length : 0,
276
+ }
277
+ }
278
+
279
+ /** 判断这个扩展名能不能直接"运行"。 */
280
+ export function runnableKind(file) {
281
+ const ext = path.extname(file).toLowerCase()
282
+ if (process.platform === 'win32') {
283
+ if (ext === '.exe' || ext === '.com') return 'exe'
284
+ if (ext === '.bat' || ext === '.cmd') return 'bat'
285
+ if (ext === '.ps1') return 'ps1'
286
+ }
287
+ if (ext === '.sh') return 'sh'
288
+ return null
289
+ }
290
+
291
+ /** 读文本文件;二进制或过大时不返回正文。 */
292
+ export function readTextFile(absPath, limit = MAX_READ) {
293
+ const target = resolvePath(absPath)
294
+ let st
295
+ try {
296
+ st = fs.statSync(target)
297
+ } catch (error) {
298
+ return { path: target, error: `打不开:${error.code ?? error.message}` }
299
+ }
300
+ if (st.isDirectory()) return { path: target, error: '这是目录' }
301
+ if (st.size > limit) {
302
+ return { path: target, sizeText: fmtSize(st.size), error: `文件太大(${fmtSize(st.size)}),超过 ${fmtSize(limit)} 不预览` }
303
+ }
304
+ const buffer = fs.readFileSync(target)
305
+ if (buffer.includes(0)) {
306
+ return { path: target, sizeText: fmtSize(st.size), binary: true, error: '二进制文件,不预览' }
307
+ }
308
+ return {
309
+ path: target,
310
+ sizeText: fmtSize(st.size),
311
+ mtime: fmtTime(st.mtime),
312
+ text: buffer.toString('utf8'),
313
+ }
314
+ }
315
+
316
+ /**
317
+ * 参数分词:支持用引号包住带空格的参数。
318
+ * '"feat: 新功能" -DryRun' → ['feat: 新功能', '-DryRun']
319
+ * 不做 shell 展开(不认 $VAR、通配符),参数原样交给被调用程序。
320
+ */
321
+ export function splitArgs(input) {
322
+ const out = [];
323
+ let current = '';
324
+ let quote = null;
325
+ for (const char of String(input ?? '')) {
326
+ if (quote !== null) {
327
+ if (char === quote) { quote = null; continue; }
328
+ current += char;
329
+ continue;
330
+ }
331
+ if (char === '"' || char === "'") { quote = char; continue; }
332
+ if (/\s/u.test(char)) {
333
+ if (current !== '') { out.push(current); current = ''; }
334
+ continue;
335
+ }
336
+ current += char;
337
+ }
338
+ if (current !== '') out.push(current);
339
+ return out;
340
+ }
341
+
342
+ /** 保存文本文件。保留原文件权限;写入前做大小上限检查。 */
343
+ export function writeTextFile(absPath, text, limit = MAX_READ) {
344
+ const target = resolvePath(absPath);
345
+ let stat = null;
346
+ try {
347
+ stat = fs.statSync(target);
348
+ } catch {
349
+ stat = null;
350
+ }
351
+ if (stat !== null && stat.isDirectory()) return { ok: false, error: '这是目录' };
352
+ const content = String(text ?? '');
353
+ if (Buffer.byteLength(content, 'utf8') > limit) return { ok: false, error: '内容太大(超过 512KB)' };
354
+ try {
355
+ fs.writeFileSync(target, content, 'utf8');
356
+ return { ok: true, size: Buffer.byteLength(content, 'utf8') };
357
+ } catch (error) {
358
+ return { ok: false, error: '写入失败:' + (error.code ?? error.message) };
359
+ }
360
+ }
361
+
362
+ /** 按类型执行文件,收集输出。不经过 shell 拼接(bat/ps1/sh 由解释器接手)。 */
363
+ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
364
+ const target = resolvePath(absPath);
365
+ const kind = runnableKind(target);
366
+ if (kind === null) return Promise.resolve({ ok: false, error: '这个文件类型不支持直接运行' });
367
+ if (!fs.existsSync(target)) return Promise.resolve({ ok: false, error: '文件不存在' });
368
+
369
+ const argList = splitArgs(args);
370
+
371
+ // 中文 Windows 的 PowerShell / cmd 默认用 GBK 输出。为了既强制 UTF-8、又保留 -File
372
+ // 的参数绑定(switch 参数不能被转成字符串),用一个包装脚本:先设 OutputEncoding,
373
+ // 再用 & 调用目标并透传参数。不能塞进 cmd /c 一行命令——路径的引号会被 cmd 吃掉。
374
+ let command = target;
375
+ let argv = argList;
376
+ if (kind === 'ps1') {
377
+ const wrapper = path.join(os.tmpdir(), 'dsh-mobile-run-ps1.ps1');
378
+ fs.writeFileSync(wrapper, [
379
+ '[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
380
+ '$target = $args[0]',
381
+ '$rest = @($args | Select-Object -Skip 1)',
382
+ '& $target @rest',
383
+ 'exit $LASTEXITCODE',
384
+ ].join('\r\n'), 'utf8');
385
+ command = 'powershell.exe';
386
+ argv = ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', wrapper, target, ...argList];
387
+ } else if (kind === 'bat') {
388
+ command = 'cmd.exe';
389
+ const tail = argList.map((item) => ' "' + item.replace(/"/gu, '""') + '"').join('');
390
+ argv = ['/d', '/c', 'chcp 65001>nul & call "' + target + '"' + tail];
391
+ } else if (kind === 'sh') {
392
+ command = 'bash';
393
+ argv = [target, ...argList];
394
+ }
395
+
396
+ return new Promise((resolve) => {
397
+ let settled = false;
398
+ const finish = (value) => {
399
+ if (settled) return;
400
+ settled = true;
401
+ resolve(value);
402
+ };
403
+
404
+ let child;
405
+ try {
406
+ child = spawn(command, argv, {
407
+ cwd: cwd ?? path.dirname(target),
408
+ windowsHide: true,
409
+ // bat 走 cmd /c 一行命令,引号由我们自己拼好;不能让 Node 再转义一遍
410
+ windowsVerbatimArguments: kind === 'bat',
411
+ stdio: ['ignore', 'pipe', 'pipe'],
412
+ });
413
+ } catch (error) {
414
+ finish({ ok: false, error: '启动失败:' + error.message });
415
+ return;
416
+ }
417
+
418
+ let truncated = false;
419
+ const chunks = [];
420
+ let total = 0;
421
+ const collect = (chunk) => {
422
+ if (total >= MAX_OUTPUT) { truncated = true; return; }
423
+ chunks.push(chunk);
424
+ total += chunk.length;
425
+ if (total > MAX_OUTPUT) truncated = true;
426
+ };
427
+ child.stdout.on('data', collect);
428
+ child.stderr.on('data', collect);
429
+
430
+ // 已强制子进程输出 UTF-8:优先按 UTF-8 解,只有出现非法序列才退回 GBK
431
+ const decode = () => {
432
+ const buffer = Buffer.concat(chunks).subarray(0, MAX_OUTPUT);
433
+ const utf8 = buffer.toString('utf8');
434
+ if (!utf8.includes('\uFFFD')) return utf8;
435
+ try {
436
+ return new TextDecoder('gbk').decode(buffer);
437
+ } catch {
438
+ return utf8;
439
+ }
440
+ };
441
+
442
+ const timer = setTimeout(() => {
443
+ try {
444
+ if (process.platform === 'win32' && child.pid) {
445
+ spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
446
+ } else {
447
+ child.kill('SIGKILL');
448
+ }
449
+ } catch { /* 已退出 */ }
450
+ finish({ ok: true, code: null, output: decode(), truncated, timedOut: true });
451
+ }, timeoutMs);
452
+
453
+ child.on('error', (error) => {
454
+ clearTimeout(timer);
455
+ finish({ ok: false, error: '启动失败:' + error.message });
456
+ });
457
+ child.on('close', (code) => {
458
+ clearTimeout(timer);
459
+ finish({ ok: true, code, output: decode(), truncated, timedOut: false });
460
+ });
461
+ });
462
+ }
463
+
464
+ export { fmtSize, fmtTime }
package/lib/git.mjs ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * git 命令执行:给 /git 页面用。
3
+ *
4
+ * 安全边界:
5
+ * - 只执行 git 本身(不做 shell 拼接),参数由 splitArgs 分词后作为 argv 传入;
6
+ * - 关掉交互:GIT_TERMINAL_PROMPT=0、GIT_ASKPASS=echo、GIT_PAGER=cat、GIT_EDITOR=true,
7
+ * 避免命令卡在等输入(页面是同步请求,卡住就是 15 秒超时);
8
+ * - 有超时与输出上限;每条命令都写审计(在路由层)。
9
+ */
10
+ import fs from 'node:fs'
11
+ import path from 'node:path'
12
+ import { spawn } from 'node:child_process'
13
+ import { splitArgs } from './files.mjs'
14
+
15
+ const MAX_OUTPUT = 64 * 1024
16
+ const DEFAULT_TIMEOUT = 20_000
17
+
18
+ /** 是不是一个 git 仓库的根目录(有 .git 目录或 gitfile) */
19
+ export function isRepo(dir) {
20
+ try {
21
+ return fs.existsSync(path.join(dir, '.git'))
22
+ } catch {
23
+ return false
24
+ }
25
+ }
26
+
27
+ /** 仓库概况:分支、是否干净、远端、最近一条提交 */
28
+ export async function repoSummary(dir) {
29
+ const [branch, status, remote, last, branches] = await Promise.all([
30
+ runGit(dir, ['rev-parse', '--abbrev-ref', 'HEAD']),
31
+ runGit(dir, ['status', '--porcelain']),
32
+ runGit(dir, ['remote', '-v']),
33
+ runGit(dir, ['log', '-1', '--pretty=%h %ad %s', '--date=format:%m-%d %H:%M']),
34
+ runGit(dir, ['branch', '-a', '--format=%(refname:short)']),
35
+ ])
36
+ const dirty = status.ok ? status.output.trim().split('\n').filter(Boolean) : []
37
+ return {
38
+ branch: branch.ok ? branch.output.trim() : '',
39
+ dirty,
40
+ remote: remote.ok ? remote.output.trim().split('\n')[0] ?? '' : '',
41
+ last: last.ok ? last.output.trim() : '',
42
+ branches: branches.ok
43
+ ? branches.output.split('\n').map((line) => line.trim()).filter((line) => line !== '' && !line.includes('HEAD'))
44
+ : [],
45
+ }
46
+ }
47
+
48
+ /** 执行 git 子命令,返回 { ok, code, output } */
49
+ export function runGit(cwd, args, { timeoutMs = DEFAULT_TIMEOUT } = {}) {
50
+ const argList = Array.isArray(args) ? args : splitArgs(args)
51
+ return new Promise((resolve) => {
52
+ let child
53
+ try {
54
+ child = spawn('git', argList, {
55
+ cwd,
56
+ windowsHide: true,
57
+ env: {
58
+ ...process.env,
59
+ GIT_TERMINAL_PROMPT: '0',
60
+ GIT_ASKPASS: 'echo',
61
+ GIT_PAGER: 'cat',
62
+ GIT_EDITOR: 'true',
63
+ LC_ALL: 'C.UTF-8',
64
+ },
65
+ stdio: ['ignore', 'pipe', 'pipe'],
66
+ })
67
+ } catch (error) {
68
+ resolve({ ok: false, error: `启动失败:${error.message}` })
69
+ return
70
+ }
71
+
72
+ let settled = false
73
+ const finish = (value) => {
74
+ if (settled) return
75
+ settled = true
76
+ resolve(value)
77
+ }
78
+
79
+ const chunks = []
80
+ let total = 0
81
+ const collect = (chunk) => {
82
+ if (total >= MAX_OUTPUT) return
83
+ chunks.push(chunk)
84
+ total += chunk.length
85
+ }
86
+ child.stdout.on('data', collect)
87
+ child.stderr.on('data', collect)
88
+
89
+ const decode = () => {
90
+ // git 的输出基本都是 ASCII/UTF-8;路径在这种环境下也很少出现 GBK
91
+ const buffer = Buffer.concat(chunks).subarray(0, MAX_OUTPUT)
92
+ const utf8 = buffer.toString('utf8')
93
+ if (!utf8.includes('\uFFFD')) return utf8
94
+ try {
95
+ return new TextDecoder('gbk').decode(buffer)
96
+ } catch {
97
+ return utf8
98
+ }
99
+ }
100
+
101
+ const timer = setTimeout(() => {
102
+ try {
103
+ if (process.platform === 'win32' && child.pid) {
104
+ spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' })
105
+ } else {
106
+ child.kill('SIGKILL')
107
+ }
108
+ } catch { /* 已退出 */ }
109
+ finish({ ok: false, timeout: true, code: null, output: decode() + '\n(超时,已终止)' })
110
+ }, timeoutMs)
111
+
112
+ child.on('error', (error) => {
113
+ clearTimeout(timer)
114
+ finish({ ok: false, error: `启动 git 失败:${error.message}` })
115
+ })
116
+ child.on('close', (code) => {
117
+ clearTimeout(timer)
118
+ const output = decode()
119
+ finish({ ok: code === 0, code, output: output === '' ? '(无输出)' : output })
120
+ })
121
+ })
122
+ }
123
+
124
+ /** 补全候选:常用子命令 + 仓库里的分支(分支放前面,切换更常用) */
125
+ const COMMON_GIT = [
126
+ 'status --short --branch',
127
+ 'log --oneline -20',
128
+ 'log --graph --oneline --all -20',
129
+ 'diff',
130
+ 'diff --stat',
131
+ 'add -A',
132
+ 'commit -m ""',
133
+ 'commit --amend --no-edit',
134
+ 'pull',
135
+ 'push',
136
+ 'fetch --all',
137
+ 'branch',
138
+ 'checkout',
139
+ 'switch',
140
+ 'stash',
141
+ 'stash pop',
142
+ 'merge',
143
+ 'rebase',
144
+ 'tag',
145
+ 'remote -v',
146
+ 'reset --soft HEAD~1',
147
+ 'restore',
148
+ 'show --stat',
149
+ ];
150
+
151
+ export function suggestionsFor(summary) {
152
+ const branches = (summary && Array.isArray(summary.branches)) ? summary.branches : [];
153
+ return [...branches, ...COMMON_GIT];
154
+ }