@yanglaofish/fishtools 0.4.0 → 0.5.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 (262) hide show
  1. package/README.md +33 -5
  2. package/bin/fst.js +1 -1
  3. package/manual/README.md +33 -5
  4. package/manual/docs/deployment.md +3 -3
  5. package/manual/docs/kernel-generation.md +97 -0
  6. package/manual/docs/modules.md +57 -0
  7. package/manual/docs/plugin-development.md +120 -10
  8. package/manual/docs/releases.md +24 -0
  9. package/manual/docs/research/2026-10-11-plugin-kernel-capabilities.md +6 -6
  10. package/manual/docs/roadmap.md +14 -3
  11. package/manual/docs/superpowers/plans/2026-10-09-workflow-mvp.md +5 -5
  12. package/manual/docs/superpowers/plans/2026-10-11-public-plugin-fixes.md +2 -2
  13. package/manual/docs/superpowers/plans/2026-10-12-kernel-generation.md +22 -0
  14. package/manual/docs/superpowers/plans/2026-10-12-modular-kernel.md +40 -0
  15. package/manual/docs/superpowers/reviews/2026-10-12-kernel-generation-review.md +27 -0
  16. package/manual/docs/superpowers/reviews/2026-10-12-modular-kernel-review.md +32 -0
  17. package/manual/docs/superpowers/reviews/2026-10-13-0.5.0-followup-review.md +67 -0
  18. package/manual/docs/superpowers/reviews/2026-10-13-kernel-runtime-release-review.md +19 -0
  19. package/manual/docs/superpowers/specs/2026-10-12-action-network-policy-design.md +52 -0
  20. package/manual/docs/superpowers/specs/2026-10-12-kernel-generation-design.md +38 -0
  21. package/manual/docs/superpowers/specs/2026-10-12-modular-kernel-design.md +70 -0
  22. package/manual/docs/superpowers/specs/2026-10-12-plugin-readiness-design.md +21 -0
  23. package/manual/docs/superpowers/specs/2026-10-12-runtime-maintenance-granularity-design.md +35 -0
  24. package/manual/docs/superpowers/specs/2026-10-12-workflow-variation-design.md +19 -0
  25. package/manual/docs/verification.md +55 -2
  26. package/manual/packages/core/README.md +44 -0
  27. package/manual/packages/core/package.json +38 -0
  28. package/manual/packages/core/src/application.js +251 -85
  29. package/manual/packages/core/src/bundle.js +2 -0
  30. package/manual/packages/core/src/development-watch-worker.js +69 -0
  31. package/manual/packages/core/src/development.js +146 -0
  32. package/manual/packages/core/src/environments.js +2 -0
  33. package/manual/packages/core/src/export-snapshot.js +42 -0
  34. package/manual/packages/core/src/plugin-update.js +108 -0
  35. package/manual/packages/core/src/process.js +2 -0
  36. package/manual/packages/core/src/resources.js +31 -0
  37. package/manual/packages/core/src/version.js +2 -0
  38. package/manual/packages/fst-cli/LICENSE +21 -0
  39. package/manual/packages/fst-cli/README.md +59 -0
  40. package/{node_modules/@mytools/core → manual/packages/fst-cli}/bin/fst.js +55 -23
  41. package/manual/packages/fst-cli/package.json +50 -0
  42. package/manual/packages/fst-cli/src/development.js +53 -0
  43. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/model.js +1 -1
  44. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/navigation.js +1 -1
  45. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/presentation.js +9 -1
  46. package/manual/packages/fst-cli/src/terminal/run-feed.js +119 -0
  47. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/run-viewer.js +9 -7
  48. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/schema.js +1 -1
  49. package/manual/packages/{core → fst-cli}/src/terminal/session.js +137 -9
  50. package/manual/packages/{core → fst-cli}/src/terminal/view.js +20 -3
  51. package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/workflow-menu.js +17 -2
  52. package/manual/packages/fst-devtools/LICENSE +21 -0
  53. package/manual/packages/fst-devtools/README.md +28 -0
  54. package/manual/packages/fst-devtools/package.json +29 -0
  55. package/{node_modules/@mytools/core → manual/packages/fst-devtools}/src/documentation.js +3 -1
  56. package/manual/packages/fst-devtools/src/plugin-quality.js +136 -0
  57. package/{node_modules/@mytools/core → manual/packages/fst-devtools}/src/plugin-tools.js +24 -5
  58. package/manual/packages/fst-logs/LICENSE +21 -0
  59. package/manual/packages/fst-logs/README.md +67 -0
  60. package/manual/packages/fst-logs/package.json +26 -0
  61. package/manual/packages/fst-logs/src/log-bounds.js +91 -0
  62. package/manual/packages/fst-logs/src/run-logs.js +224 -0
  63. package/manual/packages/fst-logs/src/run-preview.js +18 -0
  64. package/manual/packages/fst-logs/src/step-summaries.js +28 -0
  65. package/manual/packages/fst-platform/LICENSE +21 -0
  66. package/manual/packages/fst-platform/README.md +73 -0
  67. package/manual/packages/fst-platform/package.json +33 -0
  68. package/manual/packages/fst-platform/src/bundle.js +17 -0
  69. package/manual/packages/fst-platform/src/environments.js +67 -0
  70. package/manual/packages/fst-platform/src/file-probe-engine.js +183 -0
  71. package/manual/packages/fst-platform/src/file-probe-worker.js +43 -0
  72. package/manual/packages/fst-platform/src/file-probe.js +6 -0
  73. package/manual/packages/fst-platform/src/json.js +5 -0
  74. package/manual/packages/fst-platform/src/network.js +199 -0
  75. package/manual/packages/fst-platform/src/npm.js +11 -0
  76. package/manual/packages/fst-platform/src/process.js +110 -0
  77. package/manual/packages/fst-plugin/LICENSE +21 -0
  78. package/manual/packages/fst-plugin/README.md +29 -0
  79. package/manual/packages/fst-plugin/package.json +29 -0
  80. package/{node_modules/@mytools/core → manual/packages/fst-plugin}/src/contracts.js +30 -3
  81. package/manual/packages/fst-plugin/src/host.js +235 -0
  82. package/manual/packages/fst-plugin/src/loader.js +21 -0
  83. package/manual/packages/fst-plugin/src/privileges.js +91 -0
  84. package/{node_modules/@mytools/core → manual/packages/fst-plugin}/src/runtime.js +27 -8
  85. package/manual/packages/fst-workflow/LICENSE +21 -0
  86. package/manual/packages/fst-workflow/README.md +28 -0
  87. package/manual/packages/fst-workflow/package.json +24 -0
  88. package/{node_modules/@mytools/core → manual/packages/fst-workflow}/src/control.js +3 -2
  89. package/manual/packages/{core → fst-workflow}/src/engine.js +2 -2
  90. package/manual/packages/fst-workspace/LICENSE +21 -0
  91. package/manual/packages/fst-workspace/README.md +32 -0
  92. package/manual/packages/fst-workspace/package.json +34 -0
  93. package/{node_modules/@mytools/core → manual/packages/fst-workspace}/src/bundle-workspace.js +1 -1
  94. package/manual/packages/fst-workspace/src/index.js +3 -0
  95. package/{node_modules/@mytools/core → manual/packages/fst-workspace}/src/plugins.js +6 -36
  96. package/{node_modules/@mytools/core → manual/packages/fst-workspace}/src/workflows.js +40 -7
  97. package/manual/packages/pack/LICENSE +21 -0
  98. package/manual/packages/pack/README.md +31 -0
  99. package/manual/packages/pack/archive.js +15 -0
  100. package/manual/packages/pack/dependencies.js +105 -0
  101. package/manual/packages/pack/environments.js +50 -0
  102. package/manual/packages/pack/index.js +103 -0
  103. package/manual/packages/pack/package.json +29 -0
  104. package/manual/packages/pack/runtime.js +61 -0
  105. package/manual/packages/sdk/README.md +31 -0
  106. package/manual/packages/sdk/conditions.js +1 -0
  107. package/manual/packages/sdk/package.json +23 -1
  108. package/manual/plugins/web/README.md +29 -0
  109. package/manual/tests/application.test.js +6 -4
  110. package/node_modules/@mytools/core/README.md +44 -0
  111. package/node_modules/@mytools/core/package.json +17 -17
  112. package/node_modules/@mytools/core/src/application.js +251 -85
  113. package/node_modules/@mytools/core/src/bundle.js +2 -17
  114. package/node_modules/@mytools/core/src/development-watch-worker.js +69 -0
  115. package/node_modules/@mytools/core/src/development.js +146 -0
  116. package/node_modules/@mytools/core/src/environments.js +2 -67
  117. package/node_modules/@mytools/core/src/export-snapshot.js +42 -0
  118. package/node_modules/@mytools/core/src/plugin-update.js +108 -0
  119. package/node_modules/@mytools/core/src/process.js +2 -80
  120. package/node_modules/@mytools/core/src/version.js +1 -1
  121. package/node_modules/@mytools/fst-cli/LICENSE +21 -0
  122. package/node_modules/@mytools/fst-cli/README.md +59 -0
  123. package/node_modules/@mytools/fst-cli/bin/fst.js +124 -0
  124. package/node_modules/@mytools/fst-cli/package.json +50 -0
  125. package/node_modules/@mytools/fst-cli/src/cli-run.js +12 -0
  126. package/node_modules/@mytools/fst-cli/src/development.js +53 -0
  127. package/node_modules/@mytools/fst-cli/src/terminal/conditions.js +32 -0
  128. package/node_modules/@mytools/fst-cli/src/terminal/log-viewer.js +45 -0
  129. package/node_modules/@mytools/fst-cli/src/terminal/model.js +104 -0
  130. package/node_modules/@mytools/fst-cli/src/terminal/navigation.js +17 -0
  131. package/node_modules/@mytools/fst-cli/src/terminal/presentation.js +50 -0
  132. package/node_modules/@mytools/fst-cli/src/terminal/run-feed.js +119 -0
  133. package/node_modules/@mytools/fst-cli/src/terminal/run-presentation.js +60 -0
  134. package/node_modules/@mytools/fst-cli/src/terminal/run-viewer.js +106 -0
  135. package/node_modules/@mytools/fst-cli/src/terminal/schema.js +52 -0
  136. package/node_modules/@mytools/{core → fst-cli}/src/terminal/session.js +137 -9
  137. package/node_modules/@mytools/fst-cli/src/terminal/theme.js +29 -0
  138. package/node_modules/@mytools/{core → fst-cli}/src/terminal/view.js +20 -3
  139. package/node_modules/@mytools/fst-cli/src/terminal/workflow-menu.js +65 -0
  140. package/node_modules/@mytools/fst-devtools/LICENSE +21 -0
  141. package/node_modules/@mytools/fst-devtools/README.md +28 -0
  142. package/node_modules/@mytools/fst-devtools/package.json +29 -0
  143. package/node_modules/@mytools/fst-devtools/src/documentation.js +67 -0
  144. package/node_modules/@mytools/fst-devtools/src/plugin-quality.js +136 -0
  145. package/node_modules/@mytools/fst-devtools/src/plugin-tools.js +152 -0
  146. package/node_modules/@mytools/fst-logs/LICENSE +21 -0
  147. package/node_modules/@mytools/fst-logs/README.md +67 -0
  148. package/node_modules/@mytools/fst-logs/package.json +26 -0
  149. package/node_modules/@mytools/fst-logs/src/log-bounds.js +91 -0
  150. package/node_modules/@mytools/fst-logs/src/markdown-log.js +55 -0
  151. package/node_modules/@mytools/fst-logs/src/run-logs.js +224 -0
  152. package/node_modules/@mytools/fst-logs/src/run-preview.js +18 -0
  153. package/node_modules/@mytools/fst-logs/src/step-summaries.js +28 -0
  154. package/node_modules/@mytools/fst-platform/LICENSE +21 -0
  155. package/node_modules/@mytools/fst-platform/README.md +73 -0
  156. package/node_modules/@mytools/fst-platform/package.json +33 -0
  157. package/node_modules/@mytools/fst-platform/src/bundle.js +17 -0
  158. package/node_modules/@mytools/fst-platform/src/environments.js +67 -0
  159. package/node_modules/@mytools/fst-platform/src/file-probe-engine.js +183 -0
  160. package/node_modules/@mytools/fst-platform/src/file-probe-worker.js +43 -0
  161. package/node_modules/@mytools/fst-platform/src/file-probe.js +6 -0
  162. package/node_modules/@mytools/fst-platform/src/json.js +5 -0
  163. package/node_modules/@mytools/fst-platform/src/network.js +199 -0
  164. package/node_modules/@mytools/fst-platform/src/npm.js +11 -0
  165. package/node_modules/@mytools/fst-platform/src/process.js +110 -0
  166. package/node_modules/@mytools/fst-plugin/LICENSE +21 -0
  167. package/node_modules/@mytools/fst-plugin/README.md +29 -0
  168. package/node_modules/@mytools/fst-plugin/package.json +29 -0
  169. package/node_modules/@mytools/fst-plugin/src/contracts.js +86 -0
  170. package/node_modules/@mytools/fst-plugin/src/host.js +235 -0
  171. package/node_modules/@mytools/fst-plugin/src/loader.js +21 -0
  172. package/node_modules/@mytools/fst-plugin/src/privileges.js +91 -0
  173. package/{manual/packages/core → node_modules/@mytools/fst-plugin}/src/runtime.js +27 -8
  174. package/node_modules/@mytools/fst-workflow/LICENSE +21 -0
  175. package/node_modules/@mytools/fst-workflow/README.md +28 -0
  176. package/node_modules/@mytools/fst-workflow/package.json +24 -0
  177. package/node_modules/@mytools/fst-workflow/src/control.js +64 -0
  178. package/node_modules/@mytools/{core → fst-workflow}/src/engine.js +2 -2
  179. package/node_modules/@mytools/fst-workspace/LICENSE +21 -0
  180. package/node_modules/@mytools/fst-workspace/README.md +32 -0
  181. package/node_modules/@mytools/fst-workspace/package.json +34 -0
  182. package/node_modules/@mytools/fst-workspace/src/bundle-workspace.js +39 -0
  183. package/node_modules/@mytools/fst-workspace/src/index.js +3 -0
  184. package/node_modules/@mytools/fst-workspace/src/plugin-installations.js +84 -0
  185. package/node_modules/@mytools/fst-workspace/src/plugins.js +35 -0
  186. package/node_modules/@mytools/fst-workspace/src/project.js +29 -0
  187. package/node_modules/@mytools/fst-workspace/src/workflows.js +150 -0
  188. package/node_modules/@mytools/fst-workspace/templates/control-demo.yaml +47 -0
  189. package/node_modules/@mytools/fst-workspace/templates/hello.yaml +35 -0
  190. package/node_modules/@mytools/fst-workspace/templates/python-check.yaml +26 -0
  191. package/node_modules/@mytools/fst-workspace/templates/system-check.yaml +11 -0
  192. package/node_modules/@mytools/pack/README.md +31 -0
  193. package/node_modules/@mytools/pack/dependencies.js +7 -2
  194. package/node_modules/@mytools/pack/environments.js +1 -1
  195. package/node_modules/@mytools/pack/index.js +33 -25
  196. package/node_modules/@mytools/pack/package.json +29 -1
  197. package/node_modules/@mytools/sdk/README.md +31 -0
  198. package/node_modules/@mytools/sdk/conditions.js +1 -0
  199. package/node_modules/@mytools/sdk/diagnostics.js +27 -0
  200. package/node_modules/@mytools/sdk/package.json +23 -1
  201. package/node_modules/@mytools/sdk/schema-path.js +31 -0
  202. package/node_modules/@mytools/sdk/validation.js +78 -0
  203. package/node_modules/@mytools/web/README.md +29 -0
  204. package/node_modules/@mytools/web/package.json +7 -0
  205. package/node_modules/@mytools/web/public/THIRD_PARTY_NOTICES.txt +1440 -1240
  206. package/node_modules/@mytools/web/public/assets/index-BaVzHycP.js +70 -0
  207. package/node_modules/@mytools/web/public/assets/index-DA8Ae92t.css +1 -0
  208. package/node_modules/@mytools/web/public/index.html +2 -2
  209. package/node_modules/@mytools/web/server.js +24 -17
  210. package/node_modules/@mytools/web/src/model.js +84 -0
  211. package/node_modules/acorn/CHANGELOG.md +1012 -0
  212. package/node_modules/acorn/LICENSE +21 -0
  213. package/node_modules/acorn/README.md +309 -0
  214. package/node_modules/acorn/bin/acorn +4 -0
  215. package/node_modules/acorn/dist/acorn.d.mts +897 -0
  216. package/node_modules/acorn/dist/acorn.d.ts +897 -0
  217. package/node_modules/acorn/dist/acorn.js +6370 -0
  218. package/node_modules/acorn/dist/acorn.mjs +6341 -0
  219. package/node_modules/acorn/dist/bin.js +90 -0
  220. package/node_modules/acorn/package.json +50 -0
  221. package/node_modules/{minipass-flush/node_modules/minipass → tar}/node_modules/yallist/README.md +8 -7
  222. package/node_modules/tar/node_modules/yallist/package.json +68 -0
  223. package/node_modules/yallist/README.md +7 -8
  224. package/node_modules/yallist/package.json +12 -51
  225. package/package.json +131 -115
  226. package/manual/packages/core/src/host.js +0 -139
  227. package/manual/packages/core/src/terminal/run-feed.js +0 -78
  228. package/node_modules/@mytools/core/src/host.js +0 -139
  229. package/node_modules/@mytools/core/src/run-logs.js +0 -63
  230. package/node_modules/@mytools/core/src/terminal/run-feed.js +0 -78
  231. package/node_modules/@mytools/web/public/assets/index-DT2B4tMV.js +0 -70
  232. package/node_modules/@mytools/web/public/assets/index-vuZvrUTT.css +0 -1
  233. package/node_modules/minipass-flush/node_modules/minipass/node_modules/yallist/package.json +0 -29
  234. /package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/cli-run.js +0 -0
  235. /package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/conditions.js +0 -0
  236. /package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/log-viewer.js +0 -0
  237. /package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/run-presentation.js +0 -0
  238. /package/{node_modules/@mytools/core → manual/packages/fst-cli}/src/terminal/theme.js +0 -0
  239. /package/{node_modules/@mytools/core → manual/packages/fst-logs}/src/markdown-log.js +0 -0
  240. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/src/plugin-installations.js +0 -0
  241. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/src/project.js +0 -0
  242. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/templates/control-demo.yaml +0 -0
  243. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/templates/hello.yaml +0 -0
  244. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/templates/python-check.yaml +0 -0
  245. /package/{node_modules/@mytools/core → manual/packages/fst-workspace}/templates/system-check.yaml +0 -0
  246. /package/{node_modules/@mytools/core/src → manual/packages/sdk}/diagnostics.js +0 -0
  247. /package/{node_modules/@mytools/core/src → manual/packages/sdk}/schema-path.js +0 -0
  248. /package/{node_modules/@mytools/core/src → manual/packages/sdk}/validation.js +0 -0
  249. /package/node_modules/{yallist → tar/node_modules/yallist}/LICENSE.md +0 -0
  250. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/commonjs/index.d.ts +0 -0
  251. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/commonjs/index.d.ts.map +0 -0
  252. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/commonjs/index.js +0 -0
  253. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/commonjs/index.js.map +0 -0
  254. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/commonjs/package.json +0 -0
  255. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/esm/index.d.ts +0 -0
  256. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/esm/index.d.ts.map +0 -0
  257. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/esm/index.js +0 -0
  258. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/esm/index.js.map +0 -0
  259. /package/node_modules/{yallist → tar/node_modules/yallist}/dist/esm/package.json +0 -0
  260. /package/node_modules/{minipass-flush/node_modules/minipass/node_modules/yallist → yallist}/LICENSE +0 -0
  261. /package/node_modules/{minipass-flush/node_modules/minipass/node_modules/yallist → yallist}/iterator.js +0 -0
  262. /package/node_modules/{minipass-flush/node_modules/minipass/node_modules/yallist → yallist}/yallist.js +0 -0
@@ -0,0 +1,183 @@
1
+ import { fork } from 'node:child_process';
2
+
3
+ const workerPath = new URL('./file-probe-worker.js', import.meta.url);
4
+ const MAX_DIRECTORY_ENTRIES = 10000;
5
+
6
+ function integer(value, name, minimum = 1, maximum = 2147483647) {
7
+ if (!Number.isSafeInteger(value) || value < minimum || value > maximum) {
8
+ throw new TypeError(`${name} 必须是 ${minimum} 到 ${maximum} 的整数`);
9
+ }
10
+ return value;
11
+ }
12
+
13
+ function failure(status, reason, error) {
14
+ return { status, incomplete: true, reason, ...(error ? { error: { code: error.code || 'PROBE_ERROR', message: error.message || String(error) } } : {}) };
15
+ }
16
+
17
+ function isRemotePath(value) {
18
+ const normalized = value.replaceAll('/', '\\');
19
+ // Windows 扩展本地路径仍可探测;显式 UNC 和网络设备路径默认跳过。
20
+ if (/^\\\\[?.]\\/u.test(normalized)) {
21
+ return /^\\\\[?.]\\UNC(?:\\|$)/iu.test(normalized)
22
+ || /^\\\\[?.]\\GLOBALROOT\\Device\\(?:Mup|LanmanRedirector|WebDavRedirector)(?:\\|$)/iu.test(normalized);
23
+ }
24
+ return /^\\\\/u.test(normalized)
25
+ || /^\\(?:\?\?\\UNC|Device\\(?:Mup|LanmanRedirector|WebDavRedirector))(?:\\|$)/iu.test(normalized);
26
+ }
27
+
28
+ /** 私有运行时边界:测试可使用真实阻塞 worker 和模拟操作系统终止失败。 */
29
+ export function createFileProbeEngine(options = {}, runtime = {}) {
30
+ const timeout = integer(options.timeout ?? 2000, 'timeout');
31
+ const maxConcurrent = integer(options.maxConcurrent ?? 2, 'maxConcurrent');
32
+ const maxPending = integer(options.maxPending ?? 16, 'maxPending', 0);
33
+ const allowRemote = options.allowRemote ?? false;
34
+ if (typeof allowRemote !== 'boolean') throw new TypeError('allowRemote 必须为布尔值');
35
+ const closeTimeout = runtime.closeTimeout ?? 250;
36
+ const terminate = runtime.terminate ?? (child => child.kill('SIGKILL'));
37
+ const active = new Set();
38
+ const pending = [];
39
+ const closeWaiters = new Set();
40
+ let closed = false;
41
+ let closing;
42
+
43
+ function status() {
44
+ return { closed, active: active.size, pending: pending.length,
45
+ unreaped: [...active].filter(record => record.unreaped).length, maxConcurrent, maxPending };
46
+ }
47
+
48
+ function finish(request, result) {
49
+ if (request.finished) return;
50
+ request.finished = true;
51
+ clearTimeout(request.timer);
52
+ request.signal?.removeEventListener('abort', request.onAbort);
53
+ const index = pending.indexOf(request);
54
+ if (index !== -1) pending.splice(index, 1);
55
+ request.resolve(result);
56
+ }
57
+
58
+ function stop(record) {
59
+ if (!active.has(record)) return;
60
+ record.unreaped = true;
61
+ try { terminate(record.child); } catch { /* 退出事件到达前继续占预算。 */ }
62
+ }
63
+
64
+ function drain() {
65
+ while (!closed && active.size < maxConcurrent && pending.length) {
66
+ start(pending.shift());
67
+ }
68
+ }
69
+
70
+ function release(record) {
71
+ if (!active.delete(record)) return;
72
+ if (!record.request.finished) {
73
+ finish(record.request, failure('error', 'workerExited', { code: 'PROBE_WORKER_EXIT', message: '文件探测进程未返回结果即退出' }));
74
+ }
75
+ drain();
76
+ if (closed && active.size === 0) for (const notify of closeWaiters) notify();
77
+ }
78
+
79
+ function start(request) {
80
+ if (request.finished) return;
81
+ let child;
82
+ try {
83
+ child = fork(runtime.workerPath ?? workerPath, [], {
84
+ execPath: runtime.execPath ?? process.execPath,
85
+ execArgv: [], windowsHide: true,
86
+ stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
87
+ });
88
+ } catch (error) {
89
+ finish(request, failure('error', 'workerStartFailed', error));
90
+ drain();
91
+ return;
92
+ }
93
+ const record = { child, request, unreaped: false };
94
+ request.record = record;
95
+ active.add(record);
96
+ child.once('exit', () => release(record));
97
+ child.once('close', () => release(record));
98
+ child.on('error', error => {
99
+ finish(request, failure('error', 'workerStartFailed', error));
100
+ if (child.pid) stop(record);
101
+ else release(record);
102
+ });
103
+ child.once('message', result => {
104
+ if (request.finished) return;
105
+ if (!result || !['ok', 'missing', 'error'].includes(result.status) || typeof result.incomplete !== 'boolean') {
106
+ finish(request, failure('error', 'workerProtocol', { code: 'PROBE_PROTOCOL', message: '文件探测进程返回非法结果' }));
107
+ stop(record);
108
+ return;
109
+ }
110
+ finish(request, result);
111
+ // 结果已到达也必须收到 exit 后才释放预算,避免新旧进程短暂重叠。
112
+ });
113
+ child.send({ operation: request.operation, path: request.path, limit: request.limit }, error => {
114
+ if (!error || request.finished) return;
115
+ finish(request, failure('error', 'workerStartFailed', error));
116
+ stop(record);
117
+ });
118
+ }
119
+
120
+ function submit(operation, path, requestOptions = {}) {
121
+ if (closed) return Promise.resolve(failure('cancelled', 'closed'));
122
+ let requestTimeout, limit, signal;
123
+ try {
124
+ if (typeof path !== 'string' || !path.length || path.includes('\0')) {
125
+ throw Object.assign(new TypeError('path 必须是非空且不含空字符的字符串'), { code: 'INVALID_PATH' });
126
+ }
127
+ requestTimeout = integer(requestOptions.timeout ?? timeout, 'timeout');
128
+ limit = operation === 'readDirectory' ? integer(requestOptions.limit ?? 1000, 'limit', 1, MAX_DIRECTORY_ENTRIES) : undefined;
129
+ signal = requestOptions.signal;
130
+ if (signal && (typeof signal.addEventListener !== 'function' || typeof signal.removeEventListener !== 'function' || typeof signal.aborted !== 'boolean')) {
131
+ throw new TypeError('signal 必须是 AbortSignal');
132
+ }
133
+ } catch (error) {
134
+ return Promise.resolve(failure('error', 'invalidOptions', error));
135
+ }
136
+ if (signal?.aborted) return Promise.resolve(failure('cancelled', 'signal'));
137
+ if (!allowRemote && isRemotePath(path)) return Promise.resolve(failure('skipped', 'remotePath'));
138
+ if (active.size >= maxConcurrent && pending.length >= maxPending) {
139
+ return Promise.resolve(failure('error', 'queueFull', { code: 'PROBE_BUSY', message: '文件探测并发和排队预算已满' }));
140
+ }
141
+ return new Promise(resolve => {
142
+ const request = { operation, path, limit, signal, resolve, finished: false };
143
+ function cancel(result) {
144
+ finish(request, result);
145
+ if (request.record) stop(request.record);
146
+ }
147
+ request.onAbort = () => cancel(failure('cancelled', 'signal'));
148
+ request.timer = setTimeout(() => cancel(failure('timedOut', 'timeout')), requestTimeout);
149
+ signal?.addEventListener('abort', request.onAbort, { once: true });
150
+ if (active.size < maxConcurrent) start(request);
151
+ else pending.push(request);
152
+ });
153
+ }
154
+
155
+ function close() {
156
+ if (closing) return closing;
157
+ closed = true;
158
+ for (const request of [...pending]) finish(request, failure('cancelled', 'closed'));
159
+ for (const record of active) {
160
+ finish(record.request, failure('cancelled', 'closed'));
161
+ stop(record);
162
+ }
163
+ closing = new Promise(resolve => {
164
+ let timer;
165
+ function done() {
166
+ clearTimeout(timer);
167
+ closeWaiters.delete(done);
168
+ // 不能立即退出的底层 I/O 仍被记录,但不会让宿主关闭无限等待。
169
+ for (const record of active) {
170
+ record.child.unref();
171
+ record.child.channel?.unref();
172
+ }
173
+ resolve({ closed: true, incomplete: active.size > 0, unreaped: active.size });
174
+ }
175
+ if (!active.size) done();
176
+ else { closeWaiters.add(done); timer = setTimeout(done, closeTimeout); }
177
+ });
178
+ return closing;
179
+ }
180
+
181
+ return { stat: (path, options) => submit('stat', path, options),
182
+ readDirectory: (path, options) => submit('readDirectory', path, options), close, status };
183
+ }
@@ -0,0 +1,43 @@
1
+ import { lstat, opendir } from 'node:fs/promises';
2
+
3
+ function type(value) {
4
+ return value.isSymbolicLink() ? 'symlink' : value.isDirectory() ? 'directory' : value.isFile() ? 'file' : 'other';
5
+ }
6
+
7
+ async function probe(request) {
8
+ if (request.operation === 'stat') {
9
+ const value = await lstat(request.path);
10
+ return { status: 'ok', incomplete: false, value: {
11
+ type: type(value), size: value.size, mtimeMs: value.mtimeMs,
12
+ ctimeMs: value.ctimeMs, birthtimeMs: value.birthtimeMs, mode: value.mode,
13
+ } };
14
+ }
15
+ if (request.operation !== 'readDirectory') throw new TypeError('未知文件探测操作');
16
+ const directory = await opendir(request.path, { bufferSize: Math.min(32, request.limit) });
17
+ const value = [];
18
+ let incomplete = false;
19
+ try {
20
+ // 不递归,也不读取文件正文;多读一个条目确认是否被上限截断。
21
+ for (let index = 0; index <= request.limit; index++) {
22
+ const entry = await directory.read();
23
+ if (!entry) break;
24
+ if (index === request.limit) { incomplete = true; break; }
25
+ value.push({ name: entry.name, type: type(entry) });
26
+ }
27
+ } finally {
28
+ await directory.close();
29
+ }
30
+ return { status: 'ok', incomplete, value, limit: request.limit, ...(incomplete ? { reason: 'entryLimit' } : {}) };
31
+ }
32
+
33
+ process.once('message', async request => {
34
+ let result;
35
+ try { result = await probe(request); }
36
+ catch (error) {
37
+ result = { status: error.code === 'ENOENT' ? 'missing' : 'error', incomplete: true,
38
+ reason: error.code === 'ENOENT' ? 'notFound' : 'filesystemError',
39
+ error: { code: error.code || 'PROBE_ERROR', message: error.message } };
40
+ }
41
+ if (!process.connected) return;
42
+ process.send(result, () => { process.disconnect(); });
43
+ });
@@ -0,0 +1,6 @@
1
+ import { createFileProbeEngine } from './file-probe-engine.js';
2
+
3
+ /** 建立只读文件探测器,实际文件操作由独立子进程完成。 */
4
+ export function createFileProbe(options = {}) {
5
+ return createFileProbeEngine(options);
6
+ }
@@ -0,0 +1,5 @@
1
+ import {readFile} from 'node:fs/promises';
2
+ export async function readJson(file,fallback) {
3
+ try { return JSON.parse(await readFile(file,'utf8')); }
4
+ catch(e) { if(e.code==='ENOENT' && fallback!==undefined) return fallback; throw e; }
5
+ }
@@ -0,0 +1,199 @@
1
+ import {Agent,EnvHttpProxyAgent,fetch as undiciFetch} from 'undici';
2
+
3
+ const privateProxy=new WeakMap();
4
+ const profileNamePattern=/^[A-Za-z][A-Za-z0-9._-]*$/;
5
+ const pluginIdPattern=/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/;
6
+ const actionIdPattern=/^[A-Za-z][A-Za-z0-9._-]*$/;
7
+ const envNamePattern=/^[A-Za-z_][A-Za-z0-9_]*$/;
8
+
9
+ function object(value,label){
10
+ if(value===null||typeof value!=='object'||Array.isArray(value))throw new TypeError(`${label} 必须是对象`);
11
+ }
12
+
13
+ function keys(value,allowed,label){
14
+ for(const key of Object.keys(value))if(!allowed.includes(key))throw new TypeError(`${label} 包含无效字段;请检查字段名称`);
15
+ }
16
+
17
+ function proxyUrl(value,label,{allowCredentials=false}={}){
18
+ let url;
19
+ try{url=new URL(value);}catch{throw new TypeError(`${label} 无效;请提供 http 或 https 代理地址`);}
20
+ if(typeof value!=='string'||!['http:','https:'].includes(url.protocol)||!url.hostname)throw new TypeError(`${label} 无效;请提供 http 或 https 代理地址`);
21
+ if(!allowCredentials&&(url.username||url.password))throw new TypeError(`${label} 无效;含凭据的代理地址请改用环境变量引用`);
22
+ return value;
23
+ }
24
+
25
+ function profileConfig(name,profile){
26
+ const label=`代理配置 ${name}`;
27
+ object(profile,label);
28
+ keys(profile,['httpUrl','httpEnv','httpsUrl','httpsEnv','noProxy','noProxyEnv'],label);
29
+ for(const [direct,reference] of [['httpUrl','httpEnv'],['httpsUrl','httpsEnv'],['noProxy','noProxyEnv']]){
30
+ if(profile[direct]!==undefined&&profile[reference]!==undefined)throw new TypeError(`${label} 的 ${direct} 和 ${reference} 不能同时设置;请只选一种来源`);
31
+ if(profile[reference]!==undefined&&(!envNamePattern.test(profile[reference])||typeof profile[reference]!=='string'))throw new TypeError(`${label} 的 ${reference} 无效;请使用环境变量名称`);
32
+ if(profile[direct]!==undefined){
33
+ if(typeof profile[direct]!=='string'||!profile[direct])throw new TypeError(`${label} 的 ${direct} 无效;请提供非空值`);
34
+ if(direct==='noProxy')continue;
35
+ proxyUrl(profile[direct],`${label} 的 ${direct}`);
36
+ }
37
+ }
38
+ if(profile.httpUrl===undefined&&profile.httpEnv===undefined&&profile.httpsUrl===undefined&&profile.httpsEnv===undefined)throw new TypeError(`${label} 缺少 HTTP 或 HTTPS 代理;请配置至少一个地址或环境变量引用`);
39
+ }
40
+
41
+ function ruleConfig(rule,label,profiles){
42
+ object(rule,label);
43
+ keys(rule,['mode','profile'],label);
44
+ if(!['inherit','direct','profile'].includes(rule.mode))throw new TypeError(`${label} 模式无效;请选择 inherit、direct 或 profile`);
45
+ if(rule.mode==='profile'){
46
+ if(typeof rule.profile!=='string'||!Object.hasOwn(profiles,rule.profile))throw new TypeError(`${label} 引用的代理配置不存在;请先创建配置`);
47
+ }else if(rule.profile!==undefined)throw new TypeError(`${label} 的配置引用无效;仅 profile 模式可引用配置`);
48
+ }
49
+
50
+ export function validateNetworkPolicy(network){
51
+ if(network===undefined)return {};
52
+ object(network,'network');
53
+ keys(network,['default','profiles','plugins'],'network');
54
+ const profiles=network.profiles??{};
55
+ object(profiles,'profiles');
56
+ for(const [name,profile] of Object.entries(profiles)){
57
+ if(!profileNamePattern.test(name))throw new TypeError('代理配置名称无效;请使用字母开头的标识');
58
+ profileConfig(name,profile);
59
+ }
60
+ if(network.default!==undefined)ruleConfig(network.default,'工作区默认规则',profiles);
61
+ const plugins=network.plugins??{};
62
+ object(plugins,'plugins');
63
+ for(const [pluginId,plugin] of Object.entries(plugins)){
64
+ if(!pluginIdPattern.test(pluginId))throw new TypeError('插件标识无效;请使用合法包名');
65
+ object(plugin,`插件 ${pluginId}`);
66
+ keys(plugin,['default','actions'],`插件 ${pluginId}`);
67
+ if(plugin.default!==undefined)ruleConfig(plugin.default,`插件 ${pluginId} 默认规则`,profiles);
68
+ const actions=plugin.actions??{};
69
+ object(actions,`插件 ${pluginId} 动作规则`);
70
+ for(const [actionId,rule] of Object.entries(actions)){
71
+ if(!actionIdPattern.test(actionId))throw new TypeError(`插件 ${pluginId} 的动作标识无效;请使用合法标识`);
72
+ ruleConfig(rule,`动作 ${pluginId}:${actionId}`,profiles);
73
+ }
74
+ }
75
+ return network;
76
+ }
77
+
78
+ function source(profile,profileName,field,env){
79
+ const label=`代理配置 ${profileName}`;
80
+ const direct=field==='noProxy'?'noProxy':`${field}Url`;
81
+ const reference=`${field}Env`;
82
+ if(profile[direct]!==undefined)return profile[direct];
83
+ if(profile[reference]===undefined)return undefined;
84
+ const value=env?.[profile[reference]];
85
+ if(typeof value!=='string'||(field!=='noProxy'&&!value))throw new TypeError(`${label} 缺少环境变量 ${profile[reference]};请在运行环境中设置它`);
86
+ if(field!=='noProxy')proxyUrl(value,`${label} 的环境变量 ${profile[reference]}`,{allowCredentials:true});
87
+ return value;
88
+ }
89
+
90
+ function inheritedProxy(env){
91
+ const get=name=>env?.[name.toLowerCase()]??env?.[name];
92
+ const allProxy=get('ALL_PROXY');
93
+ const httpProxy=get('HTTP_PROXY')??allProxy??'';
94
+ const httpsProxy=get('HTTPS_PROXY')??get('HTTP_PROXY')??allProxy??'';
95
+ return {httpProxy,httpsProxy,noProxy:get('NO_PROXY')??''};
96
+ }
97
+
98
+ function selectRule(selected,pluginId,actionId){
99
+ const plugin=selected.plugins?.[pluginId];
100
+ if(actionId!==undefined&&plugin?.actions?.[actionId])return {rule:plugin.actions[actionId],source:'action'};
101
+ if(plugin?.default)return {rule:plugin.default,source:'plugin'};
102
+ if(selected.default)return {rule:selected.default,source:'workspace'};
103
+ return {rule:{mode:'inherit'},source:'system'};
104
+ }
105
+
106
+ export function inspectNetworkPolicy(network,{plugins=[],actions=[]}={}){
107
+ const selected=validateNetworkPolicy(network);
108
+ const installed=new Set(plugins),available=new Set(actions);
109
+ const entry=(id,pluginId,actionId)=>{
110
+ const {rule,source}=selectRule(selected,pluginId,actionId);
111
+ return {id,mode:rule.mode,source,...(rule.profile?{profile:rule.profile}:{})};
112
+ };
113
+ const entries=[];
114
+ for(const pluginId of [...installed].sort()){
115
+ entries.push(entry(pluginId,pluginId));
116
+ for(const actionId of [...available].filter(id=>id.startsWith(`${pluginId}:`)).sort())entries.push(entry(actionId,pluginId,actionId.slice(pluginId.length+1)));
117
+ }
118
+ const stalePlugins=Object.keys(selected.plugins??{}).filter(id=>!installed.has(id)).sort();
119
+ const staleActions=[];
120
+ for(const [pluginId,rule] of Object.entries(selected.plugins??{}))if(installed.has(pluginId)){
121
+ for(const actionId of Object.keys(rule.actions??{}))if(!available.has(`${pluginId}:${actionId}`))staleActions.push(`${pluginId}:${actionId}`);
122
+ }
123
+ staleActions.sort();
124
+ return {entries,stalePlugins,staleActions};
125
+ }
126
+
127
+ export function resolveNetworkPolicy(network,{pluginId,actionId,env=process.env}={}){
128
+ const selected=validateNetworkPolicy(network);
129
+ const {rule}=selectRule(selected,pluginId,actionId);
130
+ const resolved=rule.mode==='profile'?{mode:'profile',profile:rule.profile}:{mode:rule.mode};
131
+ if(rule.mode==='profile'){
132
+ const profile=selected.profiles[rule.profile];
133
+ const httpProxy=source(profile,rule.profile,'http',env)??'';
134
+ privateProxy.set(resolved,{
135
+ httpProxy,
136
+ httpsProxy:source(profile,rule.profile,'https',env)??httpProxy,
137
+ noProxy:source(profile,rule.profile,'noProxy',env)??'',
138
+ });
139
+ }else if(rule.mode==='inherit')privateProxy.set(resolved,inheritedProxy(env));
140
+ else privateProxy.set(resolved,{});
141
+ return resolved;
142
+ }
143
+
144
+ const proxyVariableNames=new Set(['http_proxy','https_proxy','all_proxy','no_proxy']);
145
+
146
+ export function buildProcessEnv(resolved,{baseEnv=process.env,overrides={}}={}){
147
+ if(!privateProxy.has(resolved))throw new TypeError('网络策略尚未解析;请先调用 resolveNetworkPolicy');
148
+ const result={...baseEnv,...overrides};
149
+ if(resolved.mode==='inherit')return result;
150
+ for(const name of Object.keys(result))if(proxyVariableNames.has(name.toLowerCase()))delete result[name];
151
+ if(resolved.mode==='direct')return result;
152
+ const {httpProxy,httpsProxy,noProxy}=privateProxy.get(resolved);
153
+ if(httpProxy)result.HTTP_PROXY=httpProxy;
154
+ if(httpsProxy)result.HTTPS_PROXY=httpsProxy;
155
+ result.NO_PROXY=noProxy;
156
+ result.NODE_USE_ENV_PROXY='1';
157
+ return result;
158
+ }
159
+
160
+ export function createHttpClient(resolved,{signal,timeoutMs=30000}={}){
161
+ if(!privateProxy.has(resolved))throw new TypeError('网络策略尚未解析;请先调用 resolveNetworkPolicy');
162
+ const proxy=privateProxy.get(resolved);
163
+ let dispatcher;
164
+ const getDispatcher=()=>{
165
+ if(dispatcher)return dispatcher;
166
+ try{
167
+ dispatcher=resolved.mode==='direct'?new Agent():new EnvHttpProxyAgent({
168
+ httpProxy:proxy.httpProxy??'',
169
+ httpsProxy:proxy.httpsProxy??'',
170
+ noProxy:proxy.noProxy??'',
171
+ });
172
+ return dispatcher;
173
+ }catch{
174
+ const label=resolved.mode==='profile'?`配置 ${resolved.profile}`:'继承的代理环境';
175
+ throw new TypeError(`${label} 无效;请检查 HTTP_PROXY 和 HTTPS_PROXY`);
176
+ }
177
+ };
178
+ let closed=false;
179
+ return {
180
+ async fetch(input,options={}){
181
+ if(closed)throw new TypeError('HTTP 客户端已关闭');
182
+ const {timeout,timeoutMs:requestTimeoutMs,signal:requestSignal,dispatcher:ignored,...init}=options;
183
+ const requestTimeout=timeout??requestTimeoutMs??timeoutMs;
184
+ if(!Number.isSafeInteger(requestTimeout)||requestTimeout<=0)throw new TypeError('HTTP 请求超时必须是正整数毫秒数');
185
+ const combined=AbortSignal.any([signal,requestSignal,AbortSignal.timeout(requestTimeout)].filter(Boolean));
186
+ try{return await undiciFetch(input,{...init,signal:combined,dispatcher:getDispatcher()});}
187
+ catch(error){
188
+ if(combined.aborted)throw error;
189
+ const label=resolved.mode==='profile'?`(配置 ${resolved.profile})`:'';
190
+ throw new TypeError(`HTTP 请求失败${label};请检查目标地址或代理连接`);
191
+ }
192
+ },
193
+ async close(){
194
+ if(closed)return;
195
+ closed=true;
196
+ if(dispatcher)await dispatcher.destroy();
197
+ },
198
+ };
199
+ }
@@ -0,0 +1,11 @@
1
+ import path from 'node:path';
2
+ import {access} from 'node:fs/promises';
3
+ import {runProcess} from './process.js';
4
+ export async function npm(args,options={}) {
5
+ const candidates=[path.join(path.dirname(process.execPath),'node_modules/npm/bin/npm-cli.js'),process.env.npm_execpath].filter(Boolean);
6
+ for(const cli of candidates) {
7
+ try {await access(cli);} catch {continue;}
8
+ return runProcess(process.execPath,[cli,...args],{timeout:180000,...options});
9
+ }
10
+ return runProcess('npm',args,{timeout:180000,...options});
11
+ }
@@ -0,0 +1,110 @@
1
+ import { execa } from 'execa';
2
+ import treeKill from 'tree-kill';
3
+ import which from 'which';
4
+ import { FlowError } from '@mytools/sdk';
5
+ import path from 'node:path';
6
+
7
+ const outputText=bytes=>bytes??new Uint8Array();
8
+ const stripFinalNewline=text=>text.endsWith('\r\n')?text.slice(0,-2):text.endsWith('\n')?text.slice(0,-1):text;
9
+
10
+ export async function runProcess(file,args=[],{cwd,env,extendEnv=true,signal,timeout=30000,maxBuffer=4*1024*1024,encoding='utf8',onOutput,outputMode='buffer',summaryBytes=65536}={}) {
11
+ if(!['buffer','stream'].includes(outputMode))throw new TypeError('outputMode 必须为 buffer 或 stream');
12
+ const streaming=outputMode==='stream';
13
+ if(streaming&&typeof onOutput!=='function')throw new TypeError('stream 模式必须提供 onOutput 消费输出');
14
+ if(!Number.isSafeInteger(summaryBytes)||summaryBytes<0||summaryBytes>1024*1024)throw new TypeError('summaryBytes 必须为 0 到 1048576 的整数');
15
+ if(typeof encoding!=='string')throw new TypeError(`不支持的程序输出编码:${String(encoding)}`);
16
+ const label=/^(utf16le|ucs2|ucs-2)$/i.test(encoding)?'utf-16le':encoding;
17
+ let decoderEncoding;
18
+ try {decoderEncoding=new TextDecoder(label).encoding;}
19
+ catch {throw new TypeError(`不支持的程序输出编码:${encoding}`);}
20
+ if(signal?.aborted) throw new FlowError('操作已取消',{code:'CANCELLED'});
21
+ let executable;
22
+ try {
23
+ // 不改变宿主 cwd;程序路径和 PATH 中相对目录都以子进程 cwd 为基准。
24
+ const directory=path.resolve(cwd??process.cwd()),windows=process.platform==='win32';
25
+ const hasPath=path.isAbsolute(file)||file.includes('/')||(windows&&file.includes('\\'));
26
+ const search=hasPath?[path.resolve(directory,file)]:[
27
+ ...(windows?[directory]:[]),...(env?.PATH??(windows?env?.Path:undefined)??process.env.PATH??'').split(path.delimiter),
28
+ ].map(entry=>path.resolve(directory,entry.replace(/^"(.*)"$/,'$1'),file));
29
+ for(const candidate of search){
30
+ executable=await which(candidate,{nothrow:true,pathExt:env?.PATHEXT??process.env.PATHEXT});
31
+ if(executable)break;
32
+ }
33
+ if(!executable)throw Object.assign(new Error(`not found: ${file}`),{code:'ENOENT'});
34
+ }
35
+ catch(cause){throw new FlowError(`未找到程序:${file}`,{code:'ENOENT',details:{executable:file},cause});}
36
+ // Execa 收集原始字节;文本只在这里按同一编码解码,避免返回值被默认 UTF-8 解码。
37
+ const child=execa(executable,args,{cwd,env,extendEnv,reject:false,windowsHide:true,maxBuffer,buffer:!streaming,timeout:0,shell:false,encoding:'buffer',stripFinalNewline:false});
38
+ let reason,killing,outputError,outputFailed=false,outputWrites=Promise.resolve();
39
+ let markStopped;const stopped=new Promise(resolve=>{markStopped=resolve;});
40
+ const outputBytes={stdout:0,stderr:0},tails={stdout:'',stderr:''};let outputTruncated=false;
41
+ const remember=(name,message)=>{
42
+ const bytes=Buffer.from(tails[name]+message);let start=Math.max(0,bytes.length-summaryBytes);
43
+ if(start){outputTruncated=true;while(start<bytes.length&&(bytes[start]&0xc0)===0x80)start++;}
44
+ tails[name]=bytes.subarray(start).toString('utf8');
45
+ };
46
+ const stop=kind=>{
47
+ if(reason) return; reason=kind;
48
+ markStopped();
49
+ killing=new Promise(resolve=>child.pid&&child.exitCode==null&&child.signalCode==null ? treeKill(child.pid,'SIGKILL',()=>resolve()) : resolve());
50
+ };
51
+ const cancel=()=>stop('cancelled');
52
+ signal?.addEventListener('abort',cancel,{once:true});
53
+ if(signal?.aborted)cancel();
54
+ const emit=(stream,message)=>{
55
+ if(!onOutput||!message)return;
56
+ // 立即挂接拒绝处理,不能让事件回调中的异步落盘变成未处理拒绝。
57
+ outputWrites=outputWrites.then(async()=>{if(!outputFailed)await onOutput({stream,message});}).catch(error=>{
58
+ if(!outputFailed){outputFailed=true;outputError=error;stop('output-callback');}
59
+ });
60
+ return outputWrites;
61
+ };
62
+ const flush=[],readers=[];
63
+ for(const [name,stream] of [['stdout',child.stdout],['stderr',child.stderr]]){
64
+ if(streaming){
65
+ // 每条流最多挂起一个块,两条流共用消费顺序;慢消费者让管道自然背压。
66
+ readers.push((async()=>{
67
+ const decoder=new TextDecoder(decoderEncoding);
68
+ try{
69
+ for await(const chunk of stream){
70
+ if(reason)break;outputBytes[name]+=chunk.byteLength;
71
+ const message=decoder.decode(chunk,{stream:true});remember(name,message);
72
+ await Promise.race([emit(name,message),stopped]);
73
+ }
74
+ if(!reason){const message=decoder.decode();remember(name,message);await Promise.race([emit(name,message),stopped]);}
75
+ }catch(error){if(!reason){outputError=error;stop('output-callback');}}
76
+ })());
77
+ continue;
78
+ }
79
+ let bytes=0,ended=false;
80
+ const decoder=new TextDecoder(decoderEncoding);
81
+ const finish=()=>{if(!ended){ended=true;emit(name,decoder.decode());}};
82
+ flush.push(finish);stream?.once('end',finish);
83
+ stream?.on('data',chunk=>{
84
+ bytes+=Buffer.byteLength(chunk);
85
+ // 流量超限时立即回收整棵进程树,不继续扩张待落盘的日志队列。
86
+ if(bytes>maxBuffer){stop('output-limit');return;}
87
+ if(onOutput)emit(name,decoder.decode(chunk,{stream:true}));
88
+ });
89
+ }
90
+ const timer=timeout>0 ? setTimeout(()=>stop('timeout'),timeout) : null;
91
+ let result;
92
+ try {
93
+ result=await child;
94
+ } finally {
95
+ for(const finish of flush)finish();
96
+ await Promise.all(readers);
97
+ await (streaming?Promise.race([outputWrites,stopped]):outputWrites);
98
+ clearTimeout(timer);
99
+ if(killing)await killing;
100
+ signal?.removeEventListener('abort',cancel);
101
+ }
102
+ if(reason==='output-callback')throw outputError;
103
+ if(reason==='output-limit')throw new FlowError('程序输出超过上限',{code:'OUTPUT_LIMIT'});
104
+ if(reason)throw new FlowError(reason==='cancelled'?'操作已取消':'程序运行超时',{code:reason==='cancelled'?'CANCELLED':'TIMEOUT'});
105
+ if(result.isMaxBuffer)throw new FlowError('程序输出超过上限',{code:'OUTPUT_LIMIT'});
106
+ if(result.exitCode===undefined&&result.failed)throw new FlowError(`无法启动程序 ${file}:${result.shortMessage??result.message}`,{code:result.code??'SPAWN_FAILED'});
107
+ const decode=bytes=>stripFinalNewline(new TextDecoder(decoderEncoding).decode(outputText(bytes)));
108
+ if(streaming)return {exitCode:result.exitCode,stdout:stripFinalNewline(tails.stdout),stderr:stripFinalNewline(tails.stderr),outputMode,outputTruncated,outputBytes};
109
+ return {exitCode:result.exitCode,stdout:decode(result.stdout),stderr:decode(result.stderr)};
110
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mytools contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # 插件模块
2
+
3
+ 包名:`@mytools/fst-plugin`。内部 workspace 包,随 `@yanglaofish/fishtools` 统一交付。
4
+
5
+ ## 职责与边界
6
+
7
+ 插件装载、契约校验、动作宿主和服务生命周期。安装批次由 workspace 负责,开发检查由 devtools 负责。
8
+
9
+ 内部依赖:`@mytools/sdk`、`@mytools/fst-platform`。
10
+ ## 公开入口
11
+
12
+ | 导入路径 | 实现位置 |
13
+ | --- | --- |
14
+ | `@mytools/fst-plugin` | `./src/host.js` |
15
+ | `@mytools/fst-plugin/loader` | `./src/loader.js` |
16
+ | `@mytools/fst-plugin/contracts` | `./src/contracts.js` |
17
+ | `@mytools/fst-plugin/package.json` | `./package.json` |
18
+
19
+ 仅依赖 exports 声明的入口,禁止消费者跨包读取 src/bin 私有文件。入口路径由本模块维护,修改协议时同步消费方回归。
20
+
21
+ ## 维护与验证
22
+
23
+ 本模块负责上述职责的实现、文档与回归;跨模块接口调整需要同时检查生产者与消费者。具体维护人员尚未指定。
24
+
25
+ 在仓库根目录执行:
26
+
27
+ ```sh
28
+ node --test tests/modules.test.js tests/plugin-contract.test.js tests/runtime.test.js tests/plugin-host-context.test.js
29
+ ```
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@mytools/fst-plugin",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "main": "src/host.js",
7
+ "exports": {
8
+ ".": "./src/host.js",
9
+ "./loader": "./src/loader.js",
10
+ "./contracts": "./src/contracts.js",
11
+ "./privileges": "./src/privileges.js",
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "license": "MIT",
20
+ "engines": {
21
+ "node": ">=24.15.0"
22
+ },
23
+ "dependencies": {
24
+ "@mytools/sdk": "0.1.0",
25
+ "@mytools/fst-platform": "0.1.0",
26
+ "cordis": "3.18.1",
27
+ "semver": "7.7.4"
28
+ }
29
+ }