@binblink/wasmnest-plugin-sdk-desktop 0.0.1 → 0.0.2

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 (219) hide show
  1. package/README.md +24 -4
  2. package/README_en.md +1 -1
  3. package/bin/cli.js +5 -5
  4. package/bin/manifest-gen.js +4 -0
  5. package/bin/manifest-validate.js +68 -6
  6. package/bin/permission-vocabulary.json +2 -2
  7. package/dev-shell/README.md +6 -5
  8. package/dev-shell/src/loader.ts +0 -5
  9. package/dev-shell/src/mock/commands.ts +1 -1
  10. package/dev-shell/src/mock/domain-context.ts +1 -1
  11. package/dev-shell/src/shell/types.ts +21 -32
  12. package/dev-shell/src/styles/shell.css +208 -2
  13. package/dev-shell/src/styles/style.css +72 -26
  14. package/dist/index.d.ts +44 -3
  15. package/dist/index.js +69 -1
  16. package/dist/types.d.ts +52 -5
  17. package/go/.gitignore +5 -0
  18. package/go/README.md +80 -0
  19. package/go/gen/wasmnest/api-call/host-api-call/empty.s +3 -0
  20. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wasm.go +13 -0
  21. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wit.go +34 -0
  22. package/go/gen/wasmnest/app/host-app/empty.s +3 -0
  23. package/go/gen/wasmnest/app/host-app/host-app.wasm.go +29 -0
  24. package/go/gen/wasmnest/app/host-app/host-app.wit.go +108 -0
  25. package/go/gen/wasmnest/auth-center/host-auth-center/abi.go +14 -0
  26. package/go/gen/wasmnest/auth-center/host-auth-center/empty.s +3 -0
  27. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wasm.go +49 -0
  28. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wit.go +225 -0
  29. package/go/gen/wasmnest/clipboard/host-clipboard/abi.go +14 -0
  30. package/go/gen/wasmnest/clipboard/host-clipboard/empty.s +3 -0
  31. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wasm.go +17 -0
  32. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wit.go +68 -0
  33. package/go/gen/wasmnest/connection-registry/host-connection-registry/empty.s +3 -0
  34. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wasm.go +13 -0
  35. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wit.go +50 -0
  36. package/go/gen/wasmnest/crypto/host-crypto/abi.go +18 -0
  37. package/go/gen/wasmnest/crypto/host-crypto/empty.s +3 -0
  38. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wasm.go +37 -0
  39. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wit.go +139 -0
  40. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/abi.go +14 -0
  41. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/empty.s +3 -0
  42. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wasm.go +21 -0
  43. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wit.go +62 -0
  44. package/go/gen/wasmnest/http/host-http/abi.go +14 -0
  45. package/go/gen/wasmnest/http/host-http/empty.s +3 -0
  46. package/go/gen/wasmnest/http/host-http/host-http.wasm.go +13 -0
  47. package/go/gen/wasmnest/http/host-http/host-http.wit.go +40 -0
  48. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/empty.s +3 -0
  49. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wasm.go +17 -0
  50. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wit.go +69 -0
  51. package/go/gen/wasmnest/mdns/host-mdns/empty.s +3 -0
  52. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wasm.go +29 -0
  53. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wit.go +101 -0
  54. package/go/gen/wasmnest/notify/host-notify/empty.s +3 -0
  55. package/go/gen/wasmnest/notify/host-notify/host-notify.wasm.go +13 -0
  56. package/go/gen/wasmnest/notify/host-notify/host-notify.wit.go +64 -0
  57. package/go/gen/wasmnest/peer/host-peer/abi.go +14 -0
  58. package/go/gen/wasmnest/peer/host-peer/empty.s +3 -0
  59. package/go/gen/wasmnest/peer/host-peer/host-peer.wasm.go +85 -0
  60. package/go/gen/wasmnest/peer/host-peer/host-peer.wit.go +357 -0
  61. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/empty.s +3 -0
  62. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wasm.go +21 -0
  63. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wit.go +70 -0
  64. package/go/gen/wasmnest/plugin/abi-form/abi-form.exports.go +11 -0
  65. package/go/gen/wasmnest/plugin/abi-form/abi-form.wasm.go +13 -0
  66. package/go/gen/wasmnest/plugin/abi-form/abi-form.wit.go +7 -0
  67. package/go/gen/wasmnest/plugin/abi-form/empty.s +3 -0
  68. package/go/gen/wasmnest/plugin/command/command.exports.go +11 -0
  69. package/go/gen/wasmnest/plugin/command/command.wasm.go +19 -0
  70. package/go/gen/wasmnest/plugin/command/command.wit.go +6 -0
  71. package/go/gen/wasmnest/plugin/command/empty.s +3 -0
  72. package/go/gen/wasmnest/plugin/events/empty.s +3 -0
  73. package/go/gen/wasmnest/plugin/events/events.exports.go +52 -0
  74. package/go/gen/wasmnest/plugin/events/events.wasm.go +74 -0
  75. package/go/gen/wasmnest/plugin/events/events.wit.go +195 -0
  76. package/go/gen/wasmnest/plugin/host-bus/empty.s +3 -0
  77. package/go/gen/wasmnest/plugin/host-bus/host-bus.wasm.go +29 -0
  78. package/go/gen/wasmnest/plugin/host-bus/host-bus.wit.go +94 -0
  79. package/go/gen/wasmnest/plugin/host-config/abi.go +14 -0
  80. package/go/gen/wasmnest/plugin/host-config/empty.s +3 -0
  81. package/go/gen/wasmnest/plugin/host-config/host-config.wasm.go +13 -0
  82. package/go/gen/wasmnest/plugin/host-config/host-config.wit.go +21 -0
  83. package/go/gen/wasmnest/plugin/host-events/empty.s +3 -0
  84. package/go/gen/wasmnest/plugin/host-events/host-events.wasm.go +9 -0
  85. package/go/gen/wasmnest/plugin/host-events/host-events.wit.go +27 -0
  86. package/go/gen/wasmnest/plugin/host-fs/abi.go +14 -0
  87. package/go/gen/wasmnest/plugin/host-fs/empty.s +3 -0
  88. package/go/gen/wasmnest/plugin/host-fs/host-fs.wasm.go +33 -0
  89. package/go/gen/wasmnest/plugin/host-fs/host-fs.wit.go +86 -0
  90. package/go/gen/wasmnest/plugin/host-log/empty.s +3 -0
  91. package/go/gen/wasmnest/plugin/host-log/host-log.wasm.go +25 -0
  92. package/go/gen/wasmnest/plugin/host-log/host-log.wit.go +65 -0
  93. package/go/gen/wasmnest/plugin/host-metrics/empty.s +3 -0
  94. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wasm.go +13 -0
  95. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wit.go +34 -0
  96. package/go/gen/wasmnest/plugin/host-platform/empty.s +3 -0
  97. package/go/gen/wasmnest/plugin/host-platform/host-platform.wasm.go +21 -0
  98. package/go/gen/wasmnest/plugin/host-platform/host-platform.wit.go +85 -0
  99. package/go/gen/wasmnest/plugin/host-plugin-database/abi.go +14 -0
  100. package/go/gen/wasmnest/plugin/host-plugin-database/empty.s +3 -0
  101. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wasm.go +29 -0
  102. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wit.go +70 -0
  103. package/go/gen/wasmnest/plugin/host-storage/abi.go +14 -0
  104. package/go/gen/wasmnest/plugin/host-storage/empty.s +3 -0
  105. package/go/gen/wasmnest/plugin/host-storage/host-storage.wasm.go +21 -0
  106. package/go/gen/wasmnest/plugin/host-storage/host-storage.wit.go +44 -0
  107. package/go/gen/wasmnest/plugin/host-worker/empty.s +3 -0
  108. package/go/gen/wasmnest/plugin/host-worker/host-worker.wasm.go +17 -0
  109. package/go/gen/wasmnest/plugin/host-worker/host-worker.wit.go +50 -0
  110. package/go/gen/wasmnest/plugin/lifecycle/empty.s +3 -0
  111. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.exports.go +32 -0
  112. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wasm.go +41 -0
  113. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wit.go +9 -0
  114. package/go/gen/wasmnest/plugin/manifest/empty.s +3 -0
  115. package/go/gen/wasmnest/plugin/manifest/manifest.exports.go +11 -0
  116. package/go/gen/wasmnest/plugin/manifest/manifest.wasm.go +13 -0
  117. package/go/gen/wasmnest/plugin/manifest/manifest.wit.go +6 -0
  118. package/go/gen/wasmnest/plugin/plugin/plugin.wit.go +4 -0
  119. package/go/gen/wasmnest/process/host-process/empty.s +3 -0
  120. package/go/gen/wasmnest/process/host-process/host-process.wasm.go +21 -0
  121. package/go/gen/wasmnest/process/host-process/host-process.wit.go +56 -0
  122. package/go/gen/wasmnest/pty/host-pty/abi.go +14 -0
  123. package/go/gen/wasmnest/pty/host-pty/empty.s +3 -0
  124. package/go/gen/wasmnest/pty/host-pty/host-pty.wasm.go +33 -0
  125. package/go/gen/wasmnest/pty/host-pty/host-pty.wit.go +164 -0
  126. package/go/gen/wasmnest/task/host-task/abi.go +14 -0
  127. package/go/gen/wasmnest/task/host-task/empty.s +3 -0
  128. package/go/gen/wasmnest/task/host-task/host-task.wasm.go +29 -0
  129. package/go/gen/wasmnest/task/host-task/host-task.wit.go +110 -0
  130. package/go/gen/wasmnest/timer/host-timer/empty.s +3 -0
  131. package/go/gen/wasmnest/timer/host-timer/host-timer.wasm.go +13 -0
  132. package/go/gen/wasmnest/timer/host-timer/host-timer.wit.go +25 -0
  133. package/go/gen/wasmnest/ws/host-websocket/empty.s +3 -0
  134. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wasm.go +29 -0
  135. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wit.go +109 -0
  136. package/go/gen/wasmnest/ws-server/host-websocket-server/empty.s +3 -0
  137. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wasm.go +49 -0
  138. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wit.go +220 -0
  139. package/go/go.mod +14 -0
  140. package/go/go.sum +2 -0
  141. package/go/sdk/dispatch.go +49 -0
  142. package/go/sdk/dispatch_test.go +81 -0
  143. package/go/sdk/entry_native.go +13 -0
  144. package/go/sdk/entry_wasip2.go +188 -0
  145. package/go/sdk/host_http_wasip2.go +42 -0
  146. package/go/sdk/host_wasip2.go +251 -0
  147. package/go/sdk/plugin.go +103 -0
  148. package/go/testplugin/index.js +1 -0
  149. package/go/testplugin/main.go +67 -0
  150. package/go/testplugin/plugin.json +24 -0
  151. package/go/tools/assemble-wit.mjs +152 -0
  152. package/go/tools/gen-bindings.mjs +51 -0
  153. package/go/wit/core.wit +273 -0
  154. package/go/wit/deps/api-call/cap-api-call.wit +20 -0
  155. package/go/wit/deps/app/cap-app.wit +54 -0
  156. package/go/wit/deps/auth-center/cap-auth-center.wit +124 -0
  157. package/go/wit/deps/clipboard/clipboard.wit +48 -0
  158. package/go/wit/deps/clocks/monotonic-clock.wit +45 -0
  159. package/go/wit/deps/clocks/wall-clock.wit +42 -0
  160. package/go/wit/deps/clocks/world.wit +6 -0
  161. package/go/wit/deps/connection-registry/cap-connection-registry.wit +40 -0
  162. package/go/wit/deps/crypto/cap-crypto.wit +68 -0
  163. package/go/wit/deps/filesystem/preopens.wit +8 -0
  164. package/go/wit/deps/filesystem/types.wit +634 -0
  165. package/go/wit/deps/filesystem/world.wit +6 -0
  166. package/go/wit/deps/fs-desktop/cap-fs-desktop.wit +29 -0
  167. package/go/wit/deps/http/http.wit +28 -0
  168. package/go/wit/deps/http-endpoint/cap-http.wit +44 -0
  169. package/go/wit/deps/io/error.wit +34 -0
  170. package/go/wit/deps/io/poll.wit +41 -0
  171. package/go/wit/deps/io/streams.wit +262 -0
  172. package/go/wit/deps/io/world.wit +6 -0
  173. package/go/wit/deps/mdns/mdns.wit +45 -0
  174. package/go/wit/deps/notify/notify.wit +51 -0
  175. package/go/wit/deps/peer/peer.wit +126 -0
  176. package/go/wit/deps/platform-desktop/cap-platform-desktop.wit +38 -0
  177. package/go/wit/deps/process/cap-process.wit +25 -0
  178. package/go/wit/deps/pty/pty.wit +80 -0
  179. package/go/wit/deps/random/insecure-seed.wit +25 -0
  180. package/go/wit/deps/random/insecure.wit +22 -0
  181. package/go/wit/deps/random/random.wit +26 -0
  182. package/go/wit/deps/random/world.wit +7 -0
  183. package/go/wit/deps/sockets/instance-network.wit +9 -0
  184. package/go/wit/deps/sockets/ip-name-lookup.wit +51 -0
  185. package/go/wit/deps/sockets/network.wit +145 -0
  186. package/go/wit/deps/sockets/tcp-create-socket.wit +27 -0
  187. package/go/wit/deps/sockets/tcp.wit +353 -0
  188. package/go/wit/deps/sockets/udp-create-socket.wit +27 -0
  189. package/go/wit/deps/sockets/udp.wit +266 -0
  190. package/go/wit/deps/sockets/world.wit +11 -0
  191. package/go/wit/deps/task/cap-task.wit +56 -0
  192. package/go/wit/deps/timer/cap-timer.wit +14 -0
  193. package/go/wit/deps/wasi-cli/command.wit +7 -0
  194. package/go/wit/deps/wasi-cli/environment.wit +18 -0
  195. package/go/wit/deps/wasi-cli/exit.wit +4 -0
  196. package/go/wit/deps/wasi-cli/imports.wit +20 -0
  197. package/go/wit/deps/wasi-cli/run.wit +4 -0
  198. package/go/wit/deps/wasi-cli/stdio.wit +17 -0
  199. package/go/wit/deps/wasi-cli/terminal.wit +49 -0
  200. package/go/wit/deps/ws/ws.wit +50 -0
  201. package/go/wit/deps/ws-server/cap-ws.wit +99 -0
  202. package/go/wit/go-plugin.wit +28 -0
  203. package/go/wit/plugin-face.wit +355 -0
  204. package/go/wit/wasmnest.wit +25 -0
  205. package/package.json +12 -1
  206. package/src/index.ts +8 -0
  207. package/src/types.ts +681 -630
  208. package/src/ui/AppUpdateSection.d.ts +44 -0
  209. package/src/ui/AppUpdateSection.vue +253 -0
  210. package/src/ui/NumberField.d.ts +49 -0
  211. package/src/ui/NumberField.vue +390 -0
  212. package/src/ui/Select.vue +4 -4
  213. package/src/update.ts +123 -0
  214. package/template/README.md +39 -39
  215. package/tools/componentize/Cargo.lock +30 -1
  216. package/tools/componentize/Cargo.toml +3 -0
  217. package/tools/componentize/src/main.rs +197 -94
  218. package/dev-shell/src/mock/agent-hub-commands.ts +0 -1065
  219. package/dev-shell/src/mock/agent-hub.ts +0 -42
@@ -1,94 +1,197 @@
1
- //! 插件组件化工具(迁移阶段 B 构建链)
2
- //!
3
- //! 将 wit-bindgen 产出的 core module(含 `component-type` 自定义段)编码为
4
- //! Component Model 组件,等价于 `wasm-tools component new`:
5
- //! - 产物已是组件(魔法字节 `0d 00 01 00`)时直接复制(幂等,支持增量构建)
6
- //! - 编码失败的输入(无组件元数据)给出明确错误
7
- //!
8
- //! 用法:`componentize <input.wasm> -o <output.wasm>`
9
- //! 插件构建脚本(wasm-apps/*/scripts/build.js)在 cargo build 后调用本工具。
10
-
11
- use std::process::ExitCode;
12
-
13
- fn main() -> ExitCode {
14
- let args: Vec<String> = std::env::args().collect();
15
- let mut input: Option<String> = None;
16
- let mut output: Option<String> = None;
17
-
18
- let mut i = 1;
19
- while i < args.len() {
20
- match args[i].as_str() {
21
- "-o" | "--output" => {
22
- if i + 1 < args.len() {
23
- output = Some(args[i + 1].clone());
24
- i += 1;
25
- } else {
26
- eprintln!("Missing value after {}", args[i]);
27
- return ExitCode::FAILURE;
28
- }
29
- }
30
- a if a.starts_with('-') => {
31
- eprintln!("Unknown option: {}", a);
32
- return ExitCode::FAILURE;
33
- }
34
- a => input = Some(a.to_string()),
35
- }
36
- i += 1;
37
- }
38
-
39
- let (Some(input), Some(output)) = (input, output) else {
40
- eprintln!("Usage: componentize <input.wasm> -o <output.wasm>");
41
- return ExitCode::FAILURE;
42
- };
43
-
44
- let bytes = match std::fs::read(&input) {
45
- Ok(b) => b,
46
- Err(e) => {
47
- eprintln!("Failed to read {}: {}", input, e);
48
- return ExitCode::FAILURE;
49
- }
50
- };
51
-
52
- // 幂等:产物已是组件(版本字 0d 00 01 00)则直接复制
53
- if bytes.len() >= 8 && &bytes[0..4] == b"\0asm" && &bytes[4..8] == [0x0d, 0x00, 0x01, 0x00] {
54
- return match std::fs::write(&output, &bytes) {
55
- Ok(()) => {
56
- println!("componentize: {} already a component, copied to {}", input, output);
57
- ExitCode::SUCCESS
58
- }
59
- Err(e) => {
60
- eprintln!("Failed to write {}: {}", output, e);
61
- ExitCode::FAILURE
62
- }
63
- };
64
- }
65
-
66
- let mut encoder = match wit_component::ComponentEncoder::default().module(&bytes) {
67
- Ok(e) => e,
68
- Err(e) => {
69
- eprintln!(
70
- "componentize: {} is not a componentizable core module (missing WIT metadata): {}",
71
- input, e
72
- );
73
- return ExitCode::FAILURE;
74
- }
75
- };
76
- let encoded = match encoder.encode() {
77
- Ok(b) => b,
78
- Err(e) => {
79
- eprintln!("componentize: encode {} failed: {}", input, e);
80
- return ExitCode::FAILURE;
81
- }
82
- };
83
-
84
- match std::fs::write(&output, &encoded) {
85
- Ok(()) => {
86
- println!("componentize: {} -> {} ({} bytes)", input, output, encoded.len());
87
- ExitCode::SUCCESS
88
- }
89
- Err(e) => {
90
- eprintln!("Failed to write {}: {}", output, e);
91
- ExitCode::FAILURE
92
- }
93
- }
94
- }
1
+ //! 插件组件化工具(迁移阶段 B 构建链)
2
+ //!
3
+ //! 将 wit-bindgen 产出的 core module(含 `component-type` 自定义段)编码为
4
+ //! Component Model 组件,等价于 `wasm-tools component new`:
5
+ //! - 产物已是组件(魔法字节 `0d 00 01 00`)时直接复制(幂等,支持增量构建)
6
+ //! - 编码失败的输入(无组件元数据)给出明确错误
7
+ //!
8
+ //! 用法:`componentize <input.wasm> -o <output.wasm>`
9
+ //! 插件构建脚本(wasm-apps/*/scripts/build.js)在 cargo build 后调用本工具。
10
+ //!
11
+ //! 另有只读模式 `componentize --list-packages <component.wasm>`:解码组件,
12
+ //! 输出其 world 实际 import / export 的 `wasmnest:` WIT 包 → 版本 JSON
13
+ //! (ADR 0061 拆包后的发布下限:`compat.witPackages` 必须来自**二进制本身**,
14
+ //! 不能从源码猜)。同一包出现两个版本即失败(组件不可能合法地同时依赖两版)。
15
+
16
+ use std::collections::BTreeMap;
17
+ use std::process::ExitCode;
18
+
19
+ use wit_parser::{WorldItem, WorldKey};
20
+
21
+ fn main() -> ExitCode {
22
+ let args: Vec<String> = std::env::args().collect();
23
+ let mut input: Option<String> = None;
24
+ let mut output: Option<String> = None;
25
+ let mut list_packages: Option<String> = None;
26
+
27
+ let mut i = 1;
28
+ while i < args.len() {
29
+ match args[i].as_str() {
30
+ "--list-packages" => {
31
+ if i + 1 < args.len() {
32
+ list_packages = Some(args[i + 1].clone());
33
+ i += 1;
34
+ } else {
35
+ eprintln!("Missing value after --list-packages");
36
+ return ExitCode::FAILURE;
37
+ }
38
+ }
39
+ "-o" | "--output" => {
40
+ if i + 1 < args.len() {
41
+ output = Some(args[i + 1].clone());
42
+ i += 1;
43
+ } else {
44
+ eprintln!("Missing value after {}", args[i]);
45
+ return ExitCode::FAILURE;
46
+ }
47
+ }
48
+ a if a.starts_with('-') => {
49
+ eprintln!("Unknown option: {}", a);
50
+ return ExitCode::FAILURE;
51
+ }
52
+ a => input = Some(a.to_string()),
53
+ }
54
+ i += 1;
55
+ }
56
+
57
+ if let Some(path) = list_packages {
58
+ return list_component_packages(&path);
59
+ }
60
+
61
+ let (Some(input), Some(output)) = (input, output) else {
62
+ eprintln!("Usage: componentize <input.wasm> -o <output.wasm>");
63
+ eprintln!(" componentize --list-packages <component.wasm>");
64
+ return ExitCode::FAILURE;
65
+ };
66
+
67
+ let bytes = match std::fs::read(&input) {
68
+ Ok(b) => b,
69
+ Err(e) => {
70
+ eprintln!("Failed to read {}: {}", input, e);
71
+ return ExitCode::FAILURE;
72
+ }
73
+ };
74
+
75
+ // 幂等:产物已是组件(版本字 0d 00 01 00)则直接复制
76
+ if bytes.len() >= 8 && &bytes[0..4] == b"\0asm" && &bytes[4..8] == [0x0d, 0x00, 0x01, 0x00] {
77
+ return match std::fs::write(&output, &bytes) {
78
+ Ok(()) => {
79
+ println!("componentize: {} already a component, copied to {}", input, output);
80
+ ExitCode::SUCCESS
81
+ }
82
+ Err(e) => {
83
+ eprintln!("Failed to write {}: {}", output, e);
84
+ ExitCode::FAILURE
85
+ }
86
+ };
87
+ }
88
+
89
+ let mut encoder = match wit_component::ComponentEncoder::default().module(&bytes) {
90
+ Ok(e) => e,
91
+ Err(e) => {
92
+ eprintln!(
93
+ "componentize: {} is not a componentizable core module (missing WIT metadata): {}",
94
+ input, e
95
+ );
96
+ return ExitCode::FAILURE;
97
+ }
98
+ };
99
+ let encoded = match encoder.encode() {
100
+ Ok(b) => b,
101
+ Err(e) => {
102
+ eprintln!("componentize: encode {} failed: {}", input, e);
103
+ return ExitCode::FAILURE;
104
+ }
105
+ };
106
+
107
+ match std::fs::write(&output, &encoded) {
108
+ Ok(()) => {
109
+ println!("componentize: {} -> {} ({} bytes)", input, output, encoded.len());
110
+ ExitCode::SUCCESS
111
+ }
112
+ Err(e) => {
113
+ eprintln!("Failed to write {}: {}", output, e);
114
+ ExitCode::FAILURE
115
+ }
116
+ }
117
+ }
118
+
119
+ /// `--list-packages`:列出组件 world 实际 import/export 的 `wasmnest:` 包 → 版本
120
+ ///
121
+ /// 只保留 `wasmnest:` 前缀(WASI 包由宿主运行时负责,不进本门禁)。输出为单行
122
+ /// JSON(键排序),供构建脚本写入 `compat.witPackages`。
123
+ fn list_component_packages(path: &str) -> ExitCode {
124
+ let bytes = match std::fs::read(path) {
125
+ Ok(b) => b,
126
+ Err(e) => {
127
+ eprintln!("Failed to read {}: {}", path, e);
128
+ return ExitCode::FAILURE;
129
+ }
130
+ };
131
+ let decoded = match wit_component::decode(&bytes) {
132
+ Ok(d) => d,
133
+ Err(e) => {
134
+ eprintln!("list-packages: decode {} failed: {}", path, e);
135
+ return ExitCode::FAILURE;
136
+ }
137
+ };
138
+ let (resolve, world_id) = match decoded {
139
+ wit_component::DecodedWasm::Component(resolve, world) => (resolve, world),
140
+ wit_component::DecodedWasm::WitPackage(..) => {
141
+ eprintln!(
142
+ "list-packages: {} 是 WIT 包而不是组件(需要构建后的 component)",
143
+ path
144
+ );
145
+ return ExitCode::FAILURE;
146
+ }
147
+ };
148
+ let world = &resolve.worlds[world_id];
149
+ let mut packages: BTreeMap<String, String> = BTreeMap::new();
150
+ let push = |iface_id: wit_parser::InterfaceId, packages: &mut BTreeMap<String, String>| {
151
+ let iface = &resolve.interfaces[iface_id];
152
+ let Some(pkg_id) = iface.package else { return };
153
+ let pkg = &resolve.packages[pkg_id].name;
154
+ if pkg.namespace != "wasmnest" {
155
+ return;
156
+ }
157
+ let name = format!("{}:{}", pkg.namespace, pkg.name);
158
+ let version = pkg
159
+ .version
160
+ .as_ref()
161
+ .map(|v| v.to_string())
162
+ .unwrap_or_default();
163
+ match packages.get(&name) {
164
+ Some(prev) if *prev != version => {
165
+ eprintln!(
166
+ "list-packages: 包 {} 在组件里出现两个版本({} 与 {})",
167
+ name, prev, version
168
+ );
169
+ // 交给外层用 Err 语义处理:这里直接 panic 不如返回标记
170
+ packages.insert(name.clone(), format!("{}!{}", prev, version));
171
+ }
172
+ Some(_) => {}
173
+ None => {
174
+ packages.insert(name, version);
175
+ }
176
+ }
177
+ };
178
+ for (key, item) in world.imports.iter().chain(world.exports.iter()) {
179
+ if let WorldKey::Interface(id) = key {
180
+ push(*id, &mut packages);
181
+ }
182
+ if let WorldItem::Interface { id, .. } = item {
183
+ push(*id, &mut packages);
184
+ }
185
+ }
186
+ if let Some((name, _)) = packages.iter().find(|(_, v)| v.contains('!')) {
187
+ eprintln!("list-packages: 包版本冲突 {}", name);
188
+ return ExitCode::FAILURE;
189
+ }
190
+ let body = packages
191
+ .iter()
192
+ .map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
193
+ .collect::<Vec<_>>()
194
+ .join(",");
195
+ println!("{{{}}}", body);
196
+ ExitCode::SUCCESS
197
+ }