@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 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.
- package/README.md +307 -2
- package/README_en.md +284 -0
- package/bin/cli.js +602 -0
- package/bin/manifest-gen.js +458 -0
- package/bin/manifest-validate.js +494 -0
- package/bin/permission-vocabulary.json +5 -0
- package/bin/plugin-watch.js +331 -0
- package/bin/wasm-hash.js +124 -0
- package/dev-shell/README.md +122 -0
- package/dev-shell/index.html +12 -0
- package/dev-shell/package.json +28 -0
- package/dev-shell/pnpm-workspace.yaml +9 -0
- package/dev-shell/postcss.config.js +6 -0
- package/dev-shell/src/App.vue +90 -0
- package/dev-shell/src/components/LogPanel.vue +90 -0
- package/dev-shell/src/loader.ts +104 -0
- package/dev-shell/src/locale.ts +29 -0
- package/dev-shell/src/locales.ts +50 -0
- package/dev-shell/src/main.ts +46 -0
- package/dev-shell/src/mock/app-env.ts +102 -0
- package/dev-shell/src/mock/commands.ts +54 -0
- package/dev-shell/src/mock/domain-context.ts +46 -0
- package/dev-shell/src/mock/events.ts +16 -0
- package/dev-shell/src/mock/file-transfer.ts +512 -0
- package/dev-shell/src/mock/tauri-ipc.ts +109 -0
- package/dev-shell/src/registry.ts +90 -0
- package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
- package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
- package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
- package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
- package/dev-shell/src/shell/mount/appSession.ts +45 -0
- package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
- package/dev-shell/src/shell/types.ts +121 -0
- package/dev-shell/src/styles/dev.css +27 -0
- package/dev-shell/src/styles/shell.css +752 -0
- package/dev-shell/src/styles/style.css +1178 -0
- package/dev-shell/src/theme.ts +140 -0
- package/dev-shell/src/vite-env.d.ts +24 -0
- package/dev-shell/tailwind.config.js +98 -0
- package/dev-shell/vite.config.ts +174 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +238 -0
- package/dist/types.d.ts +632 -0
- package/dist/types.js +0 -0
- package/dist/vite-plugin.d.ts +25 -0
- package/dist/vite-plugin.js +80 -0
- package/go/.gitignore +5 -0
- package/go/README.md +80 -0
- package/go/gen/wasmnest/api-call/host-api-call/empty.s +3 -0
- package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wasm.go +13 -0
- package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wit.go +34 -0
- package/go/gen/wasmnest/app/host-app/empty.s +3 -0
- package/go/gen/wasmnest/app/host-app/host-app.wasm.go +29 -0
- package/go/gen/wasmnest/app/host-app/host-app.wit.go +108 -0
- package/go/gen/wasmnest/auth-center/host-auth-center/abi.go +14 -0
- package/go/gen/wasmnest/auth-center/host-auth-center/empty.s +3 -0
- package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wasm.go +49 -0
- package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wit.go +225 -0
- package/go/gen/wasmnest/clipboard/host-clipboard/abi.go +14 -0
- package/go/gen/wasmnest/clipboard/host-clipboard/empty.s +3 -0
- package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wasm.go +17 -0
- package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wit.go +68 -0
- package/go/gen/wasmnest/connection-registry/host-connection-registry/empty.s +3 -0
- package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wasm.go +13 -0
- package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wit.go +50 -0
- package/go/gen/wasmnest/crypto/host-crypto/abi.go +18 -0
- package/go/gen/wasmnest/crypto/host-crypto/empty.s +3 -0
- package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wasm.go +37 -0
- package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wit.go +139 -0
- package/go/gen/wasmnest/fs-desktop/host-fs-desktop/abi.go +14 -0
- package/go/gen/wasmnest/fs-desktop/host-fs-desktop/empty.s +3 -0
- package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wasm.go +21 -0
- package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wit.go +62 -0
- package/go/gen/wasmnest/http/host-http/abi.go +14 -0
- package/go/gen/wasmnest/http/host-http/empty.s +3 -0
- package/go/gen/wasmnest/http/host-http/host-http.wasm.go +13 -0
- package/go/gen/wasmnest/http/host-http/host-http.wit.go +40 -0
- package/go/gen/wasmnest/http-endpoint/host-http-endpoint/empty.s +3 -0
- package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wasm.go +17 -0
- package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wit.go +69 -0
- package/go/gen/wasmnest/mdns/host-mdns/empty.s +3 -0
- package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wasm.go +29 -0
- package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wit.go +101 -0
- package/go/gen/wasmnest/notify/host-notify/empty.s +3 -0
- package/go/gen/wasmnest/notify/host-notify/host-notify.wasm.go +13 -0
- package/go/gen/wasmnest/notify/host-notify/host-notify.wit.go +64 -0
- package/go/gen/wasmnest/peer/host-peer/abi.go +14 -0
- package/go/gen/wasmnest/peer/host-peer/empty.s +3 -0
- package/go/gen/wasmnest/peer/host-peer/host-peer.wasm.go +85 -0
- package/go/gen/wasmnest/peer/host-peer/host-peer.wit.go +357 -0
- package/go/gen/wasmnest/platform-desktop/host-platform-desktop/empty.s +3 -0
- package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wasm.go +21 -0
- package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wit.go +70 -0
- package/go/gen/wasmnest/plugin/abi-form/abi-form.exports.go +11 -0
- package/go/gen/wasmnest/plugin/abi-form/abi-form.wasm.go +13 -0
- package/go/gen/wasmnest/plugin/abi-form/abi-form.wit.go +7 -0
- package/go/gen/wasmnest/plugin/abi-form/empty.s +3 -0
- package/go/gen/wasmnest/plugin/command/command.exports.go +11 -0
- package/go/gen/wasmnest/plugin/command/command.wasm.go +19 -0
- package/go/gen/wasmnest/plugin/command/command.wit.go +6 -0
- package/go/gen/wasmnest/plugin/command/empty.s +3 -0
- package/go/gen/wasmnest/plugin/events/empty.s +3 -0
- package/go/gen/wasmnest/plugin/events/events.exports.go +52 -0
- package/go/gen/wasmnest/plugin/events/events.wasm.go +74 -0
- package/go/gen/wasmnest/plugin/events/events.wit.go +195 -0
- package/go/gen/wasmnest/plugin/host-bus/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-bus/host-bus.wasm.go +29 -0
- package/go/gen/wasmnest/plugin/host-bus/host-bus.wit.go +94 -0
- package/go/gen/wasmnest/plugin/host-config/abi.go +14 -0
- package/go/gen/wasmnest/plugin/host-config/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-config/host-config.wasm.go +13 -0
- package/go/gen/wasmnest/plugin/host-config/host-config.wit.go +21 -0
- package/go/gen/wasmnest/plugin/host-events/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-events/host-events.wasm.go +9 -0
- package/go/gen/wasmnest/plugin/host-events/host-events.wit.go +27 -0
- package/go/gen/wasmnest/plugin/host-fs/abi.go +14 -0
- package/go/gen/wasmnest/plugin/host-fs/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-fs/host-fs.wasm.go +33 -0
- package/go/gen/wasmnest/plugin/host-fs/host-fs.wit.go +86 -0
- package/go/gen/wasmnest/plugin/host-log/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-log/host-log.wasm.go +25 -0
- package/go/gen/wasmnest/plugin/host-log/host-log.wit.go +65 -0
- package/go/gen/wasmnest/plugin/host-metrics/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wasm.go +13 -0
- package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wit.go +34 -0
- package/go/gen/wasmnest/plugin/host-platform/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-platform/host-platform.wasm.go +21 -0
- package/go/gen/wasmnest/plugin/host-platform/host-platform.wit.go +85 -0
- package/go/gen/wasmnest/plugin/host-plugin-database/abi.go +14 -0
- package/go/gen/wasmnest/plugin/host-plugin-database/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wasm.go +29 -0
- package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wit.go +70 -0
- package/go/gen/wasmnest/plugin/host-storage/abi.go +14 -0
- package/go/gen/wasmnest/plugin/host-storage/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-storage/host-storage.wasm.go +21 -0
- package/go/gen/wasmnest/plugin/host-storage/host-storage.wit.go +44 -0
- package/go/gen/wasmnest/plugin/host-worker/empty.s +3 -0
- package/go/gen/wasmnest/plugin/host-worker/host-worker.wasm.go +17 -0
- package/go/gen/wasmnest/plugin/host-worker/host-worker.wit.go +50 -0
- package/go/gen/wasmnest/plugin/lifecycle/empty.s +3 -0
- package/go/gen/wasmnest/plugin/lifecycle/lifecycle.exports.go +32 -0
- package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wasm.go +41 -0
- package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wit.go +9 -0
- package/go/gen/wasmnest/plugin/manifest/empty.s +3 -0
- package/go/gen/wasmnest/plugin/manifest/manifest.exports.go +11 -0
- package/go/gen/wasmnest/plugin/manifest/manifest.wasm.go +13 -0
- package/go/gen/wasmnest/plugin/manifest/manifest.wit.go +6 -0
- package/go/gen/wasmnest/plugin/plugin/plugin.wit.go +4 -0
- package/go/gen/wasmnest/process/host-process/empty.s +3 -0
- package/go/gen/wasmnest/process/host-process/host-process.wasm.go +21 -0
- package/go/gen/wasmnest/process/host-process/host-process.wit.go +56 -0
- package/go/gen/wasmnest/pty/host-pty/abi.go +14 -0
- package/go/gen/wasmnest/pty/host-pty/empty.s +3 -0
- package/go/gen/wasmnest/pty/host-pty/host-pty.wasm.go +33 -0
- package/go/gen/wasmnest/pty/host-pty/host-pty.wit.go +164 -0
- package/go/gen/wasmnest/task/host-task/abi.go +14 -0
- package/go/gen/wasmnest/task/host-task/empty.s +3 -0
- package/go/gen/wasmnest/task/host-task/host-task.wasm.go +29 -0
- package/go/gen/wasmnest/task/host-task/host-task.wit.go +110 -0
- package/go/gen/wasmnest/timer/host-timer/empty.s +3 -0
- package/go/gen/wasmnest/timer/host-timer/host-timer.wasm.go +13 -0
- package/go/gen/wasmnest/timer/host-timer/host-timer.wit.go +25 -0
- package/go/gen/wasmnest/ws/host-websocket/empty.s +3 -0
- package/go/gen/wasmnest/ws/host-websocket/host-websocket.wasm.go +29 -0
- package/go/gen/wasmnest/ws/host-websocket/host-websocket.wit.go +109 -0
- package/go/gen/wasmnest/ws-server/host-websocket-server/empty.s +3 -0
- package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wasm.go +49 -0
- package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wit.go +220 -0
- package/go/go.mod +14 -0
- package/go/go.sum +2 -0
- package/go/sdk/dispatch.go +49 -0
- package/go/sdk/dispatch_test.go +81 -0
- package/go/sdk/entry_native.go +13 -0
- package/go/sdk/entry_wasip2.go +188 -0
- package/go/sdk/host_http_wasip2.go +42 -0
- package/go/sdk/host_wasip2.go +251 -0
- package/go/sdk/plugin.go +103 -0
- package/go/testplugin/index.js +1 -0
- package/go/testplugin/main.go +67 -0
- package/go/testplugin/plugin.json +24 -0
- package/go/tools/assemble-wit.mjs +152 -0
- package/go/tools/gen-bindings.mjs +51 -0
- package/go/wit/core.wit +273 -0
- package/go/wit/deps/api-call/cap-api-call.wit +20 -0
- package/go/wit/deps/app/cap-app.wit +54 -0
- package/go/wit/deps/auth-center/cap-auth-center.wit +124 -0
- package/go/wit/deps/clipboard/clipboard.wit +48 -0
- package/go/wit/deps/clocks/monotonic-clock.wit +45 -0
- package/go/wit/deps/clocks/wall-clock.wit +42 -0
- package/go/wit/deps/clocks/world.wit +6 -0
- package/go/wit/deps/connection-registry/cap-connection-registry.wit +40 -0
- package/go/wit/deps/crypto/cap-crypto.wit +68 -0
- package/go/wit/deps/filesystem/preopens.wit +8 -0
- package/go/wit/deps/filesystem/types.wit +634 -0
- package/go/wit/deps/filesystem/world.wit +6 -0
- package/go/wit/deps/fs-desktop/cap-fs-desktop.wit +29 -0
- package/go/wit/deps/http/http.wit +28 -0
- package/go/wit/deps/http-endpoint/cap-http.wit +44 -0
- package/go/wit/deps/io/error.wit +34 -0
- package/go/wit/deps/io/poll.wit +41 -0
- package/go/wit/deps/io/streams.wit +262 -0
- package/go/wit/deps/io/world.wit +6 -0
- package/go/wit/deps/mdns/mdns.wit +45 -0
- package/go/wit/deps/notify/notify.wit +51 -0
- package/go/wit/deps/peer/peer.wit +126 -0
- package/go/wit/deps/platform-desktop/cap-platform-desktop.wit +38 -0
- package/go/wit/deps/process/cap-process.wit +25 -0
- package/go/wit/deps/pty/pty.wit +80 -0
- package/go/wit/deps/random/insecure-seed.wit +25 -0
- package/go/wit/deps/random/insecure.wit +22 -0
- package/go/wit/deps/random/random.wit +26 -0
- package/go/wit/deps/random/world.wit +7 -0
- package/go/wit/deps/sockets/instance-network.wit +9 -0
- package/go/wit/deps/sockets/ip-name-lookup.wit +51 -0
- package/go/wit/deps/sockets/network.wit +145 -0
- package/go/wit/deps/sockets/tcp-create-socket.wit +27 -0
- package/go/wit/deps/sockets/tcp.wit +353 -0
- package/go/wit/deps/sockets/udp-create-socket.wit +27 -0
- package/go/wit/deps/sockets/udp.wit +266 -0
- package/go/wit/deps/sockets/world.wit +11 -0
- package/go/wit/deps/task/cap-task.wit +56 -0
- package/go/wit/deps/timer/cap-timer.wit +14 -0
- package/go/wit/deps/wasi-cli/command.wit +7 -0
- package/go/wit/deps/wasi-cli/environment.wit +18 -0
- package/go/wit/deps/wasi-cli/exit.wit +4 -0
- package/go/wit/deps/wasi-cli/imports.wit +20 -0
- package/go/wit/deps/wasi-cli/run.wit +4 -0
- package/go/wit/deps/wasi-cli/stdio.wit +17 -0
- package/go/wit/deps/wasi-cli/terminal.wit +49 -0
- package/go/wit/deps/ws/ws.wit +50 -0
- package/go/wit/deps/ws-server/cap-ws.wit +99 -0
- package/go/wit/go-plugin.wit +28 -0
- package/go/wit/plugin-face.wit +355 -0
- package/go/wit/wasmnest.wit +25 -0
- package/package.json +100 -3
- package/src/config.ts +49 -0
- package/src/global-dialog.ts +169 -0
- package/src/index.ts +88 -0
- package/src/runtime.ts +54 -0
- package/src/types.ts +681 -0
- package/src/ui/AppUpdateSection.d.ts +44 -0
- package/src/ui/AppUpdateSection.vue +253 -0
- package/src/ui/MarkdownEditor.vue +520 -0
- package/src/ui/NumberField.d.ts +49 -0
- package/src/ui/NumberField.vue +390 -0
- package/src/ui/PluginGlobalDialog.vue +401 -0
- package/src/ui/Select.d.ts +36 -0
- package/src/ui/Select.vue +220 -0
- package/src/ui/TerminalInputRail.vue +184 -0
- package/src/ui/apply-select-position.ts +65 -0
- package/src/ui/markdown-editor-syntax.ts +234 -0
- package/src/ui/markdown-editor.d.ts +36 -0
- package/src/ui/plugin-global-dialog.d.ts +12 -0
- package/src/ui/select-position.ts +79 -0
- package/src/ui/terminal-input-rail.d.ts +30 -0
- package/src/ui/zoom-compensation.d.ts +9 -0
- package/src/ui/zoom-compensation.ts +52 -0
- package/src/update.ts +123 -0
- package/src/vite-plugin.ts +124 -0
- package/template/README.md +39 -0
- package/template/package.json +23 -0
- package/template/plugin.json +14 -0
- package/template/rust/Cargo.toml +21 -0
- package/template/rust/src/lib.rs +39 -0
- package/template/src/env.d.ts +24 -0
- package/template/src/index.ts +37 -0
- package/template/tsconfig.json +17 -0
- package/template/vite.config.ts +21 -0
- package/tools/componentize/Cargo.lock +299 -0
- package/tools/componentize/Cargo.toml +18 -0
- package/tools/componentize/src/main.rs +197 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Code generated by wit-bindgen-go. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
// Package hostwebsocket represents the imported interface "wasmnest:ws/host-websocket@0.0.1".
|
|
4
|
+
//
|
|
5
|
+
// ======================================================================
|
|
6
|
+
// WebSocket 客户端能力域自持契约分片(ADR 0052 D2/D4)
|
|
7
|
+
//
|
|
8
|
+
// 本文件是 `wasmnest-ws-client-engine` 持有的**共享契约真源**(world `cap-ws`
|
|
9
|
+
// 只 import 客户端域 `host-websocket`):双端 `compose.json` 的 caps.ws 直接指向
|
|
10
|
+
// 本文件(pty 先例)——契约与移动侧实现(同一 crate 的引擎)同属一处。
|
|
11
|
+
//
|
|
12
|
+
// 桌面侧客户端实现暂留 `wasmnest-server-websocket`(common-host 绑定,契约源
|
|
13
|
+
// 同为本文件;ADR 0041 D1 的已知双轨,收敛另行立项);服务端域
|
|
14
|
+
// (原 `host-websocket-server`,10 函数)是桌面扩展,声明真源在桌面
|
|
15
|
+
// `plugin-sdk-desktop/rust/wit-src/cap-ws.wit`(world `cap-ws-server`),不在本文件。
|
|
16
|
+
//
|
|
17
|
+
// 端生成物由 `scripts/compose-wit.mjs` 拼装(本文件与 core.wit 及其余 cap 分片
|
|
18
|
+
// 同 package 同版本)。分片只换绑定源、不换装配语义:`impl Host` / `HostModule` /
|
|
19
|
+
// `MODULE_*` / `DESC` 与 WIT 契约的函数集逐字一致。
|
|
20
|
+
// ======================================================================
|
|
21
|
+
// WebSocket 基础能力(v14 起;双端交集切片 = 客户端域 5 函数)
|
|
22
|
+
//
|
|
23
|
+
// 桌面独有服务端域(入站端点 10 函数,v14/v28 追加)拆入
|
|
24
|
+
// [`host-websocket-server`](cap-ws 分片,移动端不跑 WS 服务器、不跟演);
|
|
25
|
+
// 本接口只剩双端交集(出站连接域)。
|
|
26
|
+
//
|
|
27
|
+
// 事件投递(双端偏离见各端 cap 注释):状态事件经 host-bus 属主私有 topic
|
|
28
|
+
// 推送(`<owner>::ws:open|error|close`),消息帧经可选导出 events-ws 回调。
|
|
29
|
+
package hostwebsocket
|
|
30
|
+
|
|
31
|
+
import (
|
|
32
|
+
"go.bytecodealliance.org/cm"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
// Connect represents the imported function "connect".
|
|
36
|
+
//
|
|
37
|
+
// 建立出站 WS 连接:**同步阻塞至握手完成**(上限 connect-timeout-secs,
|
|
38
|
+
// 与 host-http 非流式模式同款 block_on 语义)。
|
|
39
|
+
// config-json(camelCase)纯引擎参数:
|
|
40
|
+
// `{ url, headers?, protocols?, connect-timeout-secs?, max-message-bytes? }`
|
|
41
|
+
// url **仅接受 `ws://`**(`wss://` 本期不支持,返回明确错误)。
|
|
42
|
+
// 成功 → 返回连接句柄 `wsc-<uuid>` 并发布 `<owner>::ws:open`;
|
|
43
|
+
// 失败 → 错误上抛且**不发布任何事件**(无句柄可寻址)。
|
|
44
|
+
//
|
|
45
|
+
// connect: func(config-json: string) -> result<string, string>
|
|
46
|
+
//
|
|
47
|
+
//go:nosplit
|
|
48
|
+
func Connect(configJSON string) (result cm.Result[string, string, string]) {
|
|
49
|
+
configJson0, configJson1 := cm.LowerString(configJSON)
|
|
50
|
+
wasmimport_Connect((*uint8)(configJson0), (uint32)(configJson1), &result)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// SendText represents the imported function "send-text".
|
|
55
|
+
//
|
|
56
|
+
// 发送文本帧(UTF-8);连接不存在/已关闭/发送队列满 → 错误
|
|
57
|
+
//
|
|
58
|
+
// send-text: func(handle: string, text: string) -> result<_, string>
|
|
59
|
+
//
|
|
60
|
+
//go:nosplit
|
|
61
|
+
func SendText(handle string, text string) (result cm.Result[string, struct{}, string]) {
|
|
62
|
+
handle0, handle1 := cm.LowerString(handle)
|
|
63
|
+
text0, text1 := cm.LowerString(text)
|
|
64
|
+
wasmimport_SendText((*uint8)(handle0), (uint32)(handle1), (*uint8)(text0), (uint32)(text1), &result)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// SendBinary represents the imported function "send-binary".
|
|
69
|
+
//
|
|
70
|
+
// 发送二进制帧;错误语义同 send-text
|
|
71
|
+
//
|
|
72
|
+
// send-binary: func(handle: string, payload: list<u8>) -> result<_, string>
|
|
73
|
+
//
|
|
74
|
+
//go:nosplit
|
|
75
|
+
func SendBinary(handle string, payload cm.List[uint8]) (result cm.Result[string, struct{}, string]) {
|
|
76
|
+
handle0, handle1 := cm.LowerString(handle)
|
|
77
|
+
payload0, payload1 := cm.LowerList(payload)
|
|
78
|
+
wasmimport_SendBinary((*uint8)(handle0), (uint32)(handle1), (*uint8)(payload0), (uint32)(payload1), &result)
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Close represents the imported function "close".
|
|
83
|
+
//
|
|
84
|
+
// 主动关闭连接(close-json:`{ code?, reason? }`,缺省 1000);返回是否命中。
|
|
85
|
+
// 该连接的 `ws:close.<owner>` 随即上报(wasClean=true 当且仅当 code ∈ {1000,1001})
|
|
86
|
+
//
|
|
87
|
+
// close: func(handle: string, close-json: string) -> result<bool, string>
|
|
88
|
+
//
|
|
89
|
+
//go:nosplit
|
|
90
|
+
func Close(handle string, closeJSON string) (result cm.Result[string, bool, string]) {
|
|
91
|
+
handle0, handle1 := cm.LowerString(handle)
|
|
92
|
+
closeJson0, closeJson1 := cm.LowerString(closeJSON)
|
|
93
|
+
wasmimport_Close((*uint8)(handle0), (uint32)(handle1), (*uint8)(closeJson0), (uint32)(closeJson1), &result)
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// IsConnected represents the imported function "is-connected".
|
|
98
|
+
//
|
|
99
|
+
// 查询连接是否处于 open 态(握手完成且未关闭);仅属主可查。
|
|
100
|
+
// 供插件在丢失状态事件后自愈
|
|
101
|
+
//
|
|
102
|
+
// is-connected: func(handle: string) -> result<bool, string>
|
|
103
|
+
//
|
|
104
|
+
//go:nosplit
|
|
105
|
+
func IsConnected(handle string) (result cm.Result[string, bool, string]) {
|
|
106
|
+
handle0, handle1 := cm.LowerString(handle)
|
|
107
|
+
wasmimport_IsConnected((*uint8)(handle0), (uint32)(handle1), &result)
|
|
108
|
+
return
|
|
109
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Code generated by wit-bindgen-go. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
package hostwebsocketserver
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"go.bytecodealliance.org/cm"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// This file contains wasmimport and wasmexport declarations for "wasmnest:ws-server@0.0.1".
|
|
10
|
+
|
|
11
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 register-endpoint
|
|
12
|
+
//go:noescape
|
|
13
|
+
func wasmimport_RegisterEndpoint(configJson0 *uint8, configJson1 uint32, result *cm.Result[string, string, string])
|
|
14
|
+
|
|
15
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 send-text-to-client
|
|
16
|
+
//go:noescape
|
|
17
|
+
func wasmimport_SendTextToClient(endpointId0 *uint8, endpointId1 uint32, clientId0 *uint8, clientId1 uint32, text0 *uint8, text1 uint32, result *cm.Result[string, struct{}, string])
|
|
18
|
+
|
|
19
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 send-binary-to-client
|
|
20
|
+
//go:noescape
|
|
21
|
+
func wasmimport_SendBinaryToClient(endpointId0 *uint8, endpointId1 uint32, clientId0 *uint8, clientId1 uint32, payload0 *uint8, payload1 uint32, result *cm.Result[string, struct{}, string])
|
|
22
|
+
|
|
23
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 broadcast-text
|
|
24
|
+
//go:noescape
|
|
25
|
+
func wasmimport_BroadcastText(endpointId0 *uint8, endpointId1 uint32, text0 *uint8, text1 uint32, result *cm.Result[string, uint32, string])
|
|
26
|
+
|
|
27
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 broadcast-binary
|
|
28
|
+
//go:noescape
|
|
29
|
+
func wasmimport_BroadcastBinary(endpointId0 *uint8, endpointId1 uint32, payload0 *uint8, payload1 uint32, result *cm.Result[string, uint32, string])
|
|
30
|
+
|
|
31
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 close-client
|
|
32
|
+
//go:noescape
|
|
33
|
+
func wasmimport_CloseClient(endpointId0 *uint8, endpointId1 uint32, clientId0 *uint8, clientId1 uint32, closeJson0 *uint8, closeJson1 uint32, result *cm.Result[string, bool, string])
|
|
34
|
+
|
|
35
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 unregister-endpoint
|
|
36
|
+
//go:noescape
|
|
37
|
+
func wasmimport_UnregisterEndpoint(endpointId0 *uint8, endpointId1 uint32, result *cm.Result[string, bool, string])
|
|
38
|
+
|
|
39
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 list-clients
|
|
40
|
+
//go:noescape
|
|
41
|
+
func wasmimport_ListClients(endpointId0 *uint8, endpointId1 uint32, result *cm.Result[string, string, string])
|
|
42
|
+
|
|
43
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 list-endpoints
|
|
44
|
+
//go:noescape
|
|
45
|
+
func wasmimport_ListEndpoints(result *cm.Result[string, string, string])
|
|
46
|
+
|
|
47
|
+
//go:wasmimport wasmnest:ws-server/host-websocket-server@0.0.1 connection-context
|
|
48
|
+
//go:noescape
|
|
49
|
+
func wasmimport_ConnectionContext(endpointId0 *uint8, endpointId1 uint32, clientId0 *uint8, clientId1 uint32, result *cm.Result[string, string, string])
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Code generated by wit-bindgen-go. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
// Package hostwebsocketserver represents the imported interface "wasmnest:ws-server/host-websocket-server@0.0.1".
|
|
4
|
+
//
|
|
5
|
+
// ======================================================================
|
|
6
|
+
// WebSocket 服务端域(桌面扩展分片,自 `host-websocket` 拆出)
|
|
7
|
+
//
|
|
8
|
+
// 双端功能区分的归属(ADR 0052 D3/D4):客户端域(出站连接 5 函数)是双端
|
|
9
|
+
// 功能级交集,契约与移动侧实现归能力域 crate `wasmnest-ws-client-engine`(真源
|
|
10
|
+
// `packages/wasmnest-ws-client-engine/wit/ws.wit`,world `cap-ws`;桌面侧实现
|
|
11
|
+
// 暂留 `wasmnest-server-websocket` 的双轨收敛另行立项);本 interface 只剩桌面
|
|
12
|
+
// 独有服务端域(入站端点 10 函数,v14 起 + v28 `connection-context`)。
|
|
13
|
+
// 移动端不跑 WS 服务器(ADR 0018 移动端是消费端),服务端域与「插件入站
|
|
14
|
+
// 端点」语义在本端**不存在**,不跟演(ADR 0019 双端偏离)。
|
|
15
|
+
//
|
|
16
|
+
// 属主隔离:**全部函数仅属主可调**(他人句柄/端点 → Err,
|
|
17
|
+
// `not owner of ws handle`);插件停用时宿主自动回收其全部句柄与端点。
|
|
18
|
+
// 权限:服务端域需 `ws:server`。
|
|
19
|
+
//
|
|
20
|
+
// 事件投递:状态事件经 host-bus 订阅,走**属主私有命名空间**
|
|
21
|
+
// (`<owner>::…`,跨属主订阅在宿主侧显式拒绝,他人既订不到也伪投递不进;
|
|
22
|
+
// 构造一律用 SDK `ws_event_topic`,勿手拼),标识在 payload:
|
|
23
|
+
// 服务端域 `<owner>::ws:client-connect` / `<owner>::ws:client-disconnect`
|
|
24
|
+
// 插件**须在 activate 期(或首次 register-endpoint 之前)完成订阅**:
|
|
25
|
+
// 宿主不缓冲、不重放,晚订阅期间的事件永久丢失;丢失自愈靠
|
|
26
|
+
// `list-clients` / `list-endpoints` 查询原语。
|
|
27
|
+
// 消息帧(text + binary)经可选导出 `events-ws` 回调投递。
|
|
28
|
+
//
|
|
29
|
+
// 桌面端 cap 真源:`compose.json` caps.ws-server 指向本文件,
|
|
30
|
+
// 由 `scripts/compose-wit.mjs` 复制为生成物 `rust/wit/cap-ws-server.wit`。
|
|
31
|
+
package hostwebsocketserver
|
|
32
|
+
|
|
33
|
+
import (
|
|
34
|
+
"go.bytecodealliance.org/cm"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
// RegisterEndpoint represents the imported function "register-endpoint".
|
|
38
|
+
//
|
|
39
|
+
// ==================== 服务端域(入站端点) ====================
|
|
40
|
+
// 在宿主 WS 服务器挂载插件端点(实际路径 `/ws/plugin/<plugin-id>/<path>`,
|
|
41
|
+
// 命名空间段由宿主注入,插件只提供后缀)。
|
|
42
|
+
// config-json(camelCase)纯引擎参数:
|
|
43
|
+
// `{ path, auth?, max-message-bytes?, max-clients? }`
|
|
44
|
+
// auth = "none"(默认:跳过首消息认证状态机,插件自管认证)
|
|
45
|
+
// | "jwt"(宿主校验首消息 `{"type":"auth","token":"<jwt>"}`)。
|
|
46
|
+
//
|
|
47
|
+
// 链路加密策略**按应用**决定(应用设置页 → `link_crypto::set_app_policy`),
|
|
48
|
+
// 覆盖该应用全部 HTTP / WS 端点;本注册配置不携带 `encryption` 字段
|
|
49
|
+
// (未知字段 → `Err`,严格解析)。
|
|
50
|
+
// path 非法(空、含 `/`、含 `.`、超长)或与本插件已注册端点冲突 → 错误
|
|
51
|
+
// (跨插件不冲突:路径含 plugin-id 段)。返回端点句柄 `wse-<uuid>`。
|
|
52
|
+
// 端点存在期间的每一帧都经宿主流过滤器链
|
|
53
|
+
//
|
|
54
|
+
// register-endpoint: func(config-json: string) -> result<string, string>
|
|
55
|
+
//
|
|
56
|
+
//go:nosplit
|
|
57
|
+
func RegisterEndpoint(configJSON string) (result cm.Result[string, string, string]) {
|
|
58
|
+
configJson0, configJson1 := cm.LowerString(configJSON)
|
|
59
|
+
wasmimport_RegisterEndpoint((*uint8)(configJson0), (uint32)(configJson1), &result)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// SendTextToClient represents the imported function "send-text-to-client".
|
|
64
|
+
//
|
|
65
|
+
// 向端点的指定客户端发文本帧;客户端不存在/队列满 → 错误
|
|
66
|
+
//
|
|
67
|
+
// send-text-to-client: func(endpoint-id: string, client-id: string, text: string)
|
|
68
|
+
// -> result<_, string>
|
|
69
|
+
//
|
|
70
|
+
//go:nosplit
|
|
71
|
+
func SendTextToClient(endpointID string, clientID string, text string) (result cm.Result[string, struct{}, string]) {
|
|
72
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
73
|
+
clientId0, clientId1 := cm.LowerString(clientID)
|
|
74
|
+
text0, text1 := cm.LowerString(text)
|
|
75
|
+
wasmimport_SendTextToClient((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(clientId0), (uint32)(clientId1), (*uint8)(text0), (uint32)(text1), &result)
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// SendBinaryToClient represents the imported function "send-binary-to-client".
|
|
80
|
+
//
|
|
81
|
+
// 向端点的指定客户端发二进制帧
|
|
82
|
+
//
|
|
83
|
+
// send-binary-to-client: func(endpoint-id: string, client-id: string, payload: list<u8>)
|
|
84
|
+
// -> result<_, string>
|
|
85
|
+
//
|
|
86
|
+
//go:nosplit
|
|
87
|
+
func SendBinaryToClient(endpointID string, clientID string, payload cm.List[uint8]) (result cm.Result[string, struct{}, string]) {
|
|
88
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
89
|
+
clientId0, clientId1 := cm.LowerString(clientID)
|
|
90
|
+
payload0, payload1 := cm.LowerList(payload)
|
|
91
|
+
wasmimport_SendBinaryToClient((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(clientId0), (uint32)(clientId1), (*uint8)(payload0), (uint32)(payload1), &result)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// BroadcastText represents the imported function "broadcast-text".
|
|
96
|
+
//
|
|
97
|
+
// 向端点全部客户端广播文本帧 → 成功入队客户端数(部分失败不回滚)
|
|
98
|
+
//
|
|
99
|
+
// broadcast-text: func(endpoint-id: string, text: string) -> result<u32, string>
|
|
100
|
+
//
|
|
101
|
+
//go:nosplit
|
|
102
|
+
func BroadcastText(endpointID string, text string) (result cm.Result[string, uint32, string]) {
|
|
103
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
104
|
+
text0, text1 := cm.LowerString(text)
|
|
105
|
+
wasmimport_BroadcastText((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(text0), (uint32)(text1), &result)
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// BroadcastBinary represents the imported function "broadcast-binary".
|
|
110
|
+
//
|
|
111
|
+
// 向端点全部客户端广播二进制帧 → 成功入队客户端数
|
|
112
|
+
//
|
|
113
|
+
// broadcast-binary: func(endpoint-id: string, payload: list<u8>) -> result<u32, string>
|
|
114
|
+
//
|
|
115
|
+
//go:nosplit
|
|
116
|
+
func BroadcastBinary(endpointID string, payload cm.List[uint8]) (result cm.Result[string, uint32, string]) {
|
|
117
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
118
|
+
payload0, payload1 := cm.LowerList(payload)
|
|
119
|
+
wasmimport_BroadcastBinary((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(payload0), (uint32)(payload1), &result)
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// CloseClient represents the imported function "close-client".
|
|
124
|
+
//
|
|
125
|
+
// 主动踢出端点的单个客户端(close-json:`{ code?, reason? }`,缺省 4004);
|
|
126
|
+
// 返回是否命中;随后该客户端经 `ws:client-disconnect.<owner>` 上报
|
|
127
|
+
//
|
|
128
|
+
// close-client: func(endpoint-id: string, client-id: string, close-json: string)
|
|
129
|
+
// -> result<bool, string>
|
|
130
|
+
//
|
|
131
|
+
//go:nosplit
|
|
132
|
+
func CloseClient(endpointID string, clientID string, closeJSON string) (result cm.Result[string, bool, string]) {
|
|
133
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
134
|
+
clientId0, clientId1 := cm.LowerString(clientID)
|
|
135
|
+
closeJson0, closeJson1 := cm.LowerString(closeJSON)
|
|
136
|
+
wasmimport_CloseClient((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(clientId0), (uint32)(clientId1), (*uint8)(closeJson0), (uint32)(closeJson1), &result)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// UnregisterEndpoint represents the imported function "unregister-endpoint".
|
|
141
|
+
//
|
|
142
|
+
// 关闭端点并回收句柄(含下线全部客户端,close code 4005);返回是否存在该端点
|
|
143
|
+
//
|
|
144
|
+
// unregister-endpoint: func(endpoint-id: string) -> result<bool, string>
|
|
145
|
+
//
|
|
146
|
+
//go:nosplit
|
|
147
|
+
func UnregisterEndpoint(endpointID string) (result cm.Result[string, bool, string]) {
|
|
148
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
149
|
+
wasmimport_UnregisterEndpoint((*uint8)(endpointId0), (uint32)(endpointId1), &result)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ListClients represents the imported function "list-clients".
|
|
154
|
+
//
|
|
155
|
+
// 端点在线的客户端清单 → JSON 数组
|
|
156
|
+
// `[{ clientId, addr, authenticated, connectedAt }]`(camelCase);
|
|
157
|
+
// 供插件在丢失 client-connect 事件后自愈;仅属主可查
|
|
158
|
+
//
|
|
159
|
+
// list-clients: func(endpoint-id: string) -> result<string, string>
|
|
160
|
+
//
|
|
161
|
+
//go:nosplit
|
|
162
|
+
func ListClients(endpointID string) (result cm.Result[string, string, string]) {
|
|
163
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
164
|
+
wasmimport_ListClients((*uint8)(endpointId0), (uint32)(endpointId1), &result)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ListEndpoints represents the imported function "list-endpoints".
|
|
169
|
+
//
|
|
170
|
+
// 本插件已注册端点清单 → JSON 数组 `[{ endpointId, path, clientCount }]`;
|
|
171
|
+
// 仅属主可查
|
|
172
|
+
//
|
|
173
|
+
// list-endpoints: func() -> result<string, string>
|
|
174
|
+
//
|
|
175
|
+
//go:nosplit
|
|
176
|
+
func ListEndpoints() (result cm.Result[string, string, string]) {
|
|
177
|
+
wasmimport_ListEndpoints(&result)
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ConnectionContext represents the imported function "connection-context".
|
|
182
|
+
//
|
|
183
|
+
// ==================== v28:连接上下文(安全/连接事实,零业务派生) ====================
|
|
184
|
+
// 查询端点指定客户端的**已脱敏**连接/认证上下文。
|
|
185
|
+
//
|
|
186
|
+
// 返回值只含连接/认证**事实**,不包含会话、设备在线判定、配对记录或任何业务
|
|
187
|
+
// 派生字段:
|
|
188
|
+
//
|
|
189
|
+
// {
|
|
190
|
+
// "clientId": "c-123",
|
|
191
|
+
// "endpointId": "wse-456",
|
|
192
|
+
// "owner": "com.example.plugin",
|
|
193
|
+
// "addr": "192.168.1.10:54321",
|
|
194
|
+
// "authenticated": true,
|
|
195
|
+
// "connectedAt": 1730000000000,
|
|
196
|
+
// "authContext": {
|
|
197
|
+
// "subject": "device-or-client-subject",
|
|
198
|
+
// "deviceName": "optional-name",
|
|
199
|
+
// "fingerprint": "optional-fingerprint"
|
|
200
|
+
// }
|
|
201
|
+
// }
|
|
202
|
+
//
|
|
203
|
+
// 约束:
|
|
204
|
+
// - 仅**端点属主**可调(权限 `ws:server` + 属主仲裁,跨插件查询显式拒绝);
|
|
205
|
+
// - 客户端不存在/不在该端点名下 → `Err`(`client not found in endpoint`);
|
|
206
|
+
// - **永不返回 JWT、token、公钥、私钥或配对记录**(凭据红线,AGENTS §8);
|
|
207
|
+
// - `auth: "none"` 时 `authenticated=false`,`authContext` 省略(不伪造身份);
|
|
208
|
+
// - 该原语是安全上下文查询,**不是**设备列表/在线判定/会话查询接口——
|
|
209
|
+
// 设备派生视图由插件自 `list-clients` 与本原语组合建立。
|
|
210
|
+
//
|
|
211
|
+
// connection-context: func(endpoint-id: string, client-id: string) -> result<string,
|
|
212
|
+
// string>
|
|
213
|
+
//
|
|
214
|
+
//go:nosplit
|
|
215
|
+
func ConnectionContext(endpointID string, clientID string) (result cm.Result[string, string, string]) {
|
|
216
|
+
endpointId0, endpointId1 := cm.LowerString(endpointID)
|
|
217
|
+
clientId0, clientId1 := cm.LowerString(clientID)
|
|
218
|
+
wasmimport_ConnectionContext((*uint8)(endpointId0), (uint32)(endpointId1), (*uint8)(clientId0), (uint32)(clientId1), &result)
|
|
219
|
+
return
|
|
220
|
+
}
|
package/go/go.mod
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// WasmNest 插件 SDK——Go 壳(issue #21「应用后端 Go 语言支持」Phase 1)
|
|
2
|
+
//
|
|
3
|
+
// 模块随平台仓源码取用(首期不发布,spec #21 Out of Scope):应用 Go 工程经
|
|
4
|
+
// replace 指令把本模块指到本目录,等价于 Rust 侧经 crates.io 版本 + [patch]
|
|
5
|
+
// 指回工作树 SDK 的消费形态。
|
|
6
|
+
//
|
|
7
|
+
// 绑定生成物 gen/ 与 SDK 壳 sdk/ 之外,本模块不含手工维护的契约内容——
|
|
8
|
+
// 契约真源在能力仓 WIT(经 compose-wit.mjs 拼装到 ../rust/wit/),重生成见
|
|
9
|
+
// tools/gen-bindings.mjs。
|
|
10
|
+
module github.com/7zai/wasmnest-plugin-sdk-go
|
|
11
|
+
|
|
12
|
+
go 1.27.3
|
|
13
|
+
|
|
14
|
+
require go.bytecodealliance.org/cm v0.3.0
|
package/go/go.sum
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// 命令分发表——最简形态的 [Plugin.InvokeCommand] 实现(可移植,原生可测)。
|
|
2
|
+
//
|
|
3
|
+
// 未知命令显性报错是纪律不是建议(spec #21 User Story 5:不静默返回空),
|
|
4
|
+
// [CommandTable.Invoke] 把这条纪律做成默认行为;应用自定义分发时须自行保持。
|
|
5
|
+
package sdk
|
|
6
|
+
|
|
7
|
+
import (
|
|
8
|
+
"encoding/json"
|
|
9
|
+
"fmt"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// CommandFunc 是单条命令的处理函数:原始 JSON 载荷进、任意 JSON 可序列化
|
|
13
|
+
// 值出(nil 序列化为 null,与 Rust `Value::Null` 同形)。
|
|
14
|
+
type CommandFunc func(args json.RawMessage) (any, error)
|
|
15
|
+
|
|
16
|
+
// CommandTable 是命令名到处理函数的分发表。
|
|
17
|
+
type CommandTable map[string]CommandFunc
|
|
18
|
+
|
|
19
|
+
// Invoke 分发一条命令;未知命令返回点名错误(不静默返回空)。
|
|
20
|
+
func (t CommandTable) Invoke(name string, args json.RawMessage) (any, error) {
|
|
21
|
+
fn, ok := t[name]
|
|
22
|
+
if !ok {
|
|
23
|
+
return nil, fmt.Errorf("unknown command: %s", name)
|
|
24
|
+
}
|
|
25
|
+
return fn(args)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// commandResultJSON 把命令结果序列化为宿主回传形状:成功 → 值的 JSON;
|
|
29
|
+
// 失败 → `{"error":"…"}`(错误串经 json.Marshal 转义,与 Rust 侧
|
|
30
|
+
// `serde_json::json!({ "error": e.to_string() })` 同口径,避免引号 /
|
|
31
|
+
// 反斜杠产出非法 JSON 屏蔽真实错误原因)。
|
|
32
|
+
func commandResultJSON(v any, err error) string {
|
|
33
|
+
if err != nil {
|
|
34
|
+
return mustJSON(map[string]string{"error": err.Error()})
|
|
35
|
+
}
|
|
36
|
+
return mustJSON(v)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func mustJSON(v any) string {
|
|
40
|
+
b, err := json.Marshal(v)
|
|
41
|
+
if err != nil {
|
|
42
|
+
// 序列化失败只可能是值携带 chan / func 等不可编码类型(应用侧缺陷),
|
|
43
|
+
// 点名值类型而非吞错——宿主侧拿到非法 JSON 的诊断更难。
|
|
44
|
+
// 错误串本身经 map 编码转义(字符串值恒可编码,递归必终止),
|
|
45
|
+
// 不手工拼接——拼接会把错误串里的引号 / 反斜杠变成非法 JSON。
|
|
46
|
+
return mustJSON(map[string]string{"error": "command result not JSON-serializable: " + err.Error()})
|
|
47
|
+
}
|
|
48
|
+
return string(b)
|
|
49
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// 可移植面单测(原生 go test;组件面 entry_wasm/host_wasm 由 TinyGo 构建
|
|
2
|
+
// testplugin 验证——Testing Decisions:绑定生成相关改动以产出组件为准)。
|
|
3
|
+
package sdk
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"encoding/json"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func TestCommandTableUnknownCommandFailsVisibly(t *testing.T) {
|
|
12
|
+
table := CommandTable{"ping": func(json.RawMessage) (any, error) { return "pong", nil }}
|
|
13
|
+
_, err := table.Invoke("nope", json.RawMessage(`{}`))
|
|
14
|
+
if err == nil {
|
|
15
|
+
t.Fatal("未知命令必须显性报错(不静默返回空)")
|
|
16
|
+
}
|
|
17
|
+
if !strings.Contains(err.Error(), "unknown command: nope") {
|
|
18
|
+
t.Fatalf("错误须点名未知命令,实际: %v", err)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func TestCommandTableDispatch(t *testing.T) {
|
|
23
|
+
table := CommandTable{
|
|
24
|
+
"echo": func(args json.RawMessage) (any, error) { return string(args), nil },
|
|
25
|
+
}
|
|
26
|
+
got, err := table.Invoke("echo", json.RawMessage(`{"a":1}`))
|
|
27
|
+
if err != nil {
|
|
28
|
+
t.Fatalf("echo 不应报错: %v", err)
|
|
29
|
+
}
|
|
30
|
+
if got != `{"a":1}` {
|
|
31
|
+
t.Fatalf("载荷应原样透传,实际: %v", got)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func TestCommandResultJSONErrorShape(t *testing.T) {
|
|
36
|
+
got := commandResultJSON(nil, errString(`boom "quoted" \slash`))
|
|
37
|
+
var decoded map[string]string
|
|
38
|
+
if err := json.Unmarshal([]byte(got), &decoded); err != nil {
|
|
39
|
+
t.Fatalf("错误应答必须是合法 JSON(引号/反斜杠转义): %v → %s", err, got)
|
|
40
|
+
}
|
|
41
|
+
if decoded["error"] != `boom "quoted" \slash` {
|
|
42
|
+
t.Fatalf("错误串应经转义往返不损,实际: %s", got)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func TestCommandResultJSONValueShapes(t *testing.T) {
|
|
47
|
+
cases := []struct {
|
|
48
|
+
name string
|
|
49
|
+
v any
|
|
50
|
+
want string
|
|
51
|
+
}{
|
|
52
|
+
{"nil → null(Rust Value::Null 同形)", nil, "null"},
|
|
53
|
+
{"bool", true, "true"},
|
|
54
|
+
{"对象", map[string]any{"pong": true}, `{"pong":true}`},
|
|
55
|
+
}
|
|
56
|
+
for _, c := range cases {
|
|
57
|
+
t.Run(c.name, func(t *testing.T) {
|
|
58
|
+
if got := commandResultJSON(c.v, nil); got != c.want {
|
|
59
|
+
t.Fatalf("got %s want %s", got, c.want)
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
func TestMustJSONNonSerializableStaysValidJSON(t *testing.T) {
|
|
66
|
+
got := mustJSON(map[string]any{"ch": make(chan int)})
|
|
67
|
+
var decoded map[string]string
|
|
68
|
+
if err := json.Unmarshal([]byte(got), &decoded); err != nil {
|
|
69
|
+
t.Fatalf("不可编码值的兜底应答仍须是合法 JSON: %v → %s", err, got)
|
|
70
|
+
}
|
|
71
|
+
if !strings.Contains(decoded["error"], "not JSON-serializable") {
|
|
72
|
+
t.Fatalf("兜底应点名序列化失败,实际: %s", got)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// errString 是测试用的最小 error 实现(避免引入 errors 依赖本体已经足够薄)。
|
|
77
|
+
func errString(s string) error { return &staticErr{s} }
|
|
78
|
+
|
|
79
|
+
type staticErr struct{ msg string }
|
|
80
|
+
|
|
81
|
+
func (e *staticErr) Error() string { return e.msg }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//go:build !wasip2
|
|
2
|
+
|
|
3
|
+
// 原生目标的 Entry 桩:SDK 壳只对 TinyGo wasip2 组件目标有意义(wasmimport
|
|
4
|
+
// 绑定在原生目标不可链接)。原生构建(go test / go vet)编译到本文件只为
|
|
5
|
+
// 让 sdk 包的可移植面(dispatch 等)可测;若原生代码真调用了 Entry,
|
|
6
|
+
// 显性 panic 而非静默空转——fail-visible,不提供「看似接线实则没接」的假象。
|
|
7
|
+
// (正常应用只在被 TinyGo 编译的 init() 里调用 Entry,原生路径不可达。)
|
|
8
|
+
package sdk
|
|
9
|
+
|
|
10
|
+
// Entry 在原生目标恒 panic(见文件头注)。签名与 wasm 侧逐字一致。
|
|
11
|
+
func Entry(p Plugin) {
|
|
12
|
+
panic("sdk.Entry 仅在 TinyGo wasip2 组件目标可用;原生构建只支持 sdk 包的可移植单测面")
|
|
13
|
+
}
|