@wailsnet/runtime 0.0.0-stage → 1.0.1-alpha.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 (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +113 -2
  3. package/dist/api/common.d.ts +337 -0
  4. package/dist/api/common.js +9 -0
  5. package/dist/api/common.js.map +1 -0
  6. package/dist/core/channel.d.ts +50 -0
  7. package/dist/core/channel.js +86 -0
  8. package/dist/core/channel.js.map +1 -0
  9. package/dist/core/commands.d.ts +34 -0
  10. package/dist/core/commands.js +55 -0
  11. package/dist/core/commands.js.map +1 -0
  12. package/dist/core/contextmenu.d.ts +47 -0
  13. package/dist/core/contextmenu.js +126 -0
  14. package/dist/core/contextmenu.js.map +1 -0
  15. package/dist/core/runtime.d.ts +91 -0
  16. package/dist/core/runtime.js +65 -0
  17. package/dist/core/runtime.js.map +1 -0
  18. package/dist/index.d.ts +56 -0
  19. package/dist/index.js +56 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/internal/call-error.d.ts +17 -0
  22. package/dist/internal/call-error.js +42 -0
  23. package/dist/internal/call-error.js.map +1 -0
  24. package/dist/internal/fnv1a.d.ts +14 -0
  25. package/dist/internal/fnv1a.js +23 -0
  26. package/dist/internal/fnv1a.js.map +1 -0
  27. package/dist/internal/nanoid.d.ts +17 -0
  28. package/dist/internal/nanoid.js +40 -0
  29. package/dist/internal/nanoid.js.map +1 -0
  30. package/dist/internal/stream.d.ts +135 -0
  31. package/dist/internal/stream.js +980 -0
  32. package/dist/internal/stream.js.map +1 -0
  33. package/dist/internal/transport.d.ts +111 -0
  34. package/dist/internal/transport.js +514 -0
  35. package/dist/internal/transport.js.map +1 -0
  36. package/dist/internal/types.d.ts +76 -0
  37. package/dist/internal/types.js +16 -0
  38. package/dist/internal/types.js.map +1 -0
  39. package/package.json +49 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Wails.Net 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.
package/README.md CHANGED
@@ -1,3 +1,114 @@
1
- # Temporary Holding Version
1
+ # @wailsnet/runtime
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Wails.Net(Wails v3 的 .NET 10 移植版)前端运行时 SDK,使用 **TypeScript** 编写、**pnpm** 管理。
4
+
5
+ 提供:
6
+
7
+ - **自包含 IPC 传输层**:自动检测 WebView2 / WebKitGTK / Android 原生 `postMessage`、大消息 HTTP 分块上传、Server 模式 WebSocket,以及调用取消。
8
+ - **全部核心命名空间** 与 **全部 46 个插件命令** 的强类型封装(window / dialog / clipboard / fs / http / notification / store / camera / geolocation / permissions …)。
9
+ - 与 Wails.Net 后端 `MessageProcessor` / `ResponseMessage` 协议严格一致,包括 **双层响应解包** 与 **FNV-1a 绑定 ID**。
10
+
11
+ > 与 C# 端 `Wails.Net.Generator` 的兼容:`wails.bindings.call(id, [args])` 由本包的 `wails.bindings.call` 与 `wails.bindings.id(fullName)` 提供支持。将 `TypeScriptGenerator.cs` 中的 `import { wails } from '@wails/runtime'` 改为 `@wailsnet/runtime` 即可直接复用其生成产物。
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ pnpm add @wailsnet/runtime
17
+ # 开发
18
+ pnpm install
19
+ pnpm build # tsc -> dist/ (ESM + .d.ts)
20
+ pnpm test # vitest
21
+ pnpm typecheck # tsc --noEmit
22
+ ```
23
+
24
+ > 本包仅依赖浏览器/WebView 内置 API(`fetch` / `WebSocket` / `TextEncoder` / `crypto`),**无第三方运行时依赖**。
25
+
26
+ ## 快速开始
27
+
28
+ ```ts
29
+ import { wails } from "@wailsnet/runtime";
30
+
31
+ // 调用后端绑定方法或插件命令(位置参数数组)
32
+ const greet = await wails.call<string>("GreetingService.Greet", ["World"]);
33
+
34
+ // 按 FNV-1a ID 调用(与 C# 源生成器一致)
35
+ const id = wails.bindings.id("GreetingService.Greet");
36
+ const r2 = await wails.bindings.call<string>(id, ["World"]);
37
+
38
+ // 插件命令(强类型命名空间)
39
+ await wails.clipboard.setText("hello");
40
+ const text = await wails.clipboard.getText();
41
+ await wails.dialog.message("标题", "内容");
42
+
43
+ // 可取消调用
44
+ const p = wails.http.get("https://example.com");
45
+ p.cancel(); // 向后端发送 cancel
46
+
47
+ // 事件(订阅为纯前端逻辑;62 个内建 wails:* 事件 + 自定义事件)
48
+ const off = wails.events.on("wails:window:created", (e) => console.log(e));
49
+ wails.events.emit("my:event", { foo: 1 });
50
+ off();
51
+ ```
52
+
53
+ ## 架构
54
+
55
+ ```
56
+ 前端调用 ──▶ wails.call(name, args) / wails.<ns>.<method>(...)
57
+ │
58
+ ▼
59
+ transport.invoke/send (internal/transport.ts)
60
+ │ 通道选择:
61
+ ├─ Server 模式 → WebSocket (ws://localhost:34116/wails/ws)
62
+ ├─ 原生 postMessage(≤512KB,低延迟)
63
+ │ Windows: window.chrome.webview.postMessage
64
+ │ Linux: window.webkit.messageHandlers.external.postMessage
65
+ │ Android: window.wails.invoke
66
+ └─ 其余 → HTTP POST /wails/message(>512KB 自动分块)
67
+ │
68
+ ▼
69
+ 后端 MessageProcessor → BindingManager / CommandDispatcher(插件)
70
+ │
71
+ ▼
72
+ 响应:{ id, type:"response", result:{ result, error } } ← 双层解包
73
+ 事件:{ type:"event", name, data } ← 推送至前端订阅者
74
+ ```
75
+
76
+ ### 关键协议约定(来自 AGENTS.md / MessageProcessor)
77
+
78
+ 1. **所有绑定与插件命令统一走 `type:"call"` + `{ name, args:[...] }`**,多参数必须为位置数组。
79
+ 2. **响应双层解包**:`result.result` 为业务值,`result.error` 为错误(含 `kind`:`ReferenceError` / `TypeError` / `RuntimeError`,权限拒绝表现为 `RuntimeError`)。
80
+ 3. **`event.emit` / `drag` / `contextmenu` 不返回响应** —— SDK 内部以 fire-and-forget 发送,不会创建悬挂 Promise。
81
+ 4. **调用 ID 为字符串且全局唯一**,取消依赖它作为 `callId`;`cancel` 始终走可靠通道(Server 模式走 WS,否则走 HTTP)。
82
+ 5. **`byte[]` ↔ Base64 字符串**,`Guid` ↔ 字符串,`DateTime` ↔ ISO-8601。
83
+ 6. **FNV-1a 绑定 ID** 必须与后端 `Bindings.FNV1aHash` 一致(offsetBasis=2166136261,prime=16777619,UTF-8 字节)。
84
+
85
+ ## 后端集成提示
86
+
87
+ 本包为**自包含**,不再依赖 C# 端注入的 `window.wails` / `window._wailsInvoke`。但为获知运行配置(Server 模式、各 URL、platform),它仍会读取 C# `RuntimeGenerator` 注入的轻量 `window._wails` 标志对象:
88
+
89
+ ```js
90
+ window._wails = { platform: "windows", isDebug: true, isServerMode: false,
91
+ assetServerUrl: "...", webSocketUrl: "..." };
92
+ ```
93
+
94
+ 保持该标志注入即可;前端改为 `import { wails } from "@wailsnet/runtime"`,无需再加载运行时 JS。
95
+
96
+ ## 目录结构
97
+
98
+ ```
99
+ src/
100
+ index.ts # 汇总 wails 对象与全部公共导出
101
+ core/runtime.ts # call / bindings / events / query / invoke / cancel
102
+ internal/
103
+ transport.ts # 自包含传输层(通道检测 / 分块 / WS / 取消 / 解包)
104
+ types.ts # 协议类型
105
+ fnv1a.ts # FNV-1a 哈希
106
+ call-error.ts # CallError
107
+ api/
108
+ window.ts clipboard.ts dialog.ts application.ts fs.ts
109
+ net.ts data.ts notification.ts log.ts mobile.ts common.ts
110
+ ```
111
+
112
+ ## License
113
+
114
+ MIT
@@ -0,0 +1,337 @@
1
+ /**
2
+ * 公共选项 / 数据结构类型。
3
+ *
4
+ * 这些类型与 Wails.Net 后端插件命令的参数 / 返回值对应。字段命名采用 camelCase
5
+ * (与后端 `JsonSerializerDefaults.Web` + camelCase 输出一致)。`byte[]` 在前端以
6
+ * Base64 字符串表示,`Guid` 以字符串,`DateTime` 以 ISO-8601 字符串。
7
+ */
8
+ /** 通知选项。 */
9
+ export interface NotificationOptions {
10
+ /** 标题(必填)。 */
11
+ title: string;
12
+ /** 正文(必填)。 */
13
+ body: string;
14
+ /** 图标(Base64 data URL 或路径)。 */
15
+ icon?: string;
16
+ /** 通知 ID(用于取消 / 去重)。 */
17
+ id?: string;
18
+ /** 点击动作标识。 */
19
+ action?: string;
20
+ /** 其他扩展字段。 */
21
+ [key: string]: unknown;
22
+ }
23
+ /** 打开文件对话框选项。 */
24
+ export interface OpenDialogOptions {
25
+ /** 标题。 */
26
+ title?: string;
27
+ /** 默认目录。 */
28
+ directory?: string;
29
+ /** 文件类型过滤器,如 `["*.png", "*.jpg"]`。 */
30
+ filters?: string[];
31
+ }
32
+ /** 保存文件对话框选项。 */
33
+ export interface SaveDialogOptions {
34
+ /** 标题。 */
35
+ title?: string;
36
+ /** 默认目录。 */
37
+ directory?: string;
38
+ /** 默认文件名。 */
39
+ filename?: string;
40
+ /** 文件类型过滤器。 */
41
+ filters?: string[];
42
+ }
43
+ /** 文件 / 目录状态信息。 */
44
+ export interface FileStat {
45
+ /** 名称。 */
46
+ name: string;
47
+ /** 完整路径。 */
48
+ path: string;
49
+ /** 字节大小。 */
50
+ size: number;
51
+ /** 权限模式(数值)。 */
52
+ mode: number;
53
+ /** 最后修改时间(ISO-8601 字符串)。 */
54
+ modTime: string;
55
+ /** 是否为目录。 */
56
+ isDir: boolean;
57
+ }
58
+ /** 相机采集结果。对应后端 `CameraCaptureResult`。 */
59
+ export interface CameraCaptureResult {
60
+ /** 是否成功。 */
61
+ success: boolean;
62
+ /** Base64 JPEG 图像数据;失败时为空串。 */
63
+ base64Data: string;
64
+ /** 错误信息(失败时)。 */
65
+ error?: string;
66
+ }
67
+ /** 地理坐标。对应后端 `GeolocationCoords`。 */
68
+ export interface GeoCoords {
69
+ /** 纬度。 */
70
+ latitude: number;
71
+ /** 经度。 */
72
+ longitude: number;
73
+ /** 精度(米)。 */
74
+ accuracy: number;
75
+ /** 海拔(米)。 */
76
+ altitude?: number;
77
+ /** 海拔精度(米)。 */
78
+ altitudeAccuracy?: number;
79
+ /** 航向(度)。 */
80
+ heading?: number;
81
+ /** 速度(米/秒)。 */
82
+ speed?: number;
83
+ }
84
+ /**
85
+ * 地理位置。对应后端 `GeolocationPosition`。
86
+ *
87
+ * 注意坐标在嵌套的 `coords` 字段中(与 W3C Geolocation API 一致),
88
+ * `timestamp` 为 Unix 毫秒时间戳。
89
+ */
90
+ export interface GeoPosition {
91
+ /** 坐标信息。 */
92
+ coords: GeoCoords;
93
+ /** Unix 毫秒时间戳。 */
94
+ timestamp: number;
95
+ }
96
+ /** 定位监听句柄(用于 `clearWatch`)。对应后端 `WatchPositionResult`。 */
97
+ export interface GeoWatchHandle {
98
+ /** 监听 ID。 */
99
+ watchId: number;
100
+ }
101
+ /** 触感通知类型。对应后端 `NotificationType` 枚举(驼峰字符串)。 */
102
+ export type HapticsNotificationType = "success" | "warning" | "error";
103
+ /**
104
+ * 设备信息(`device.info` 返回的 JSON 解析结果)。
105
+ * 字段取决于平台,Android 提供 platform / manufacturer / brand / model / device / version / sdkInt。
106
+ */
107
+ export interface DeviceInfo {
108
+ /** 平台标识,如 `"android"`。 */
109
+ platform?: string;
110
+ /** 制造商。 */
111
+ manufacturer?: string;
112
+ /** 品牌。 */
113
+ brand?: string;
114
+ /** 型号。 */
115
+ model?: string;
116
+ /** 设备代号。 */
117
+ device?: string;
118
+ /** 系统版本号。 */
119
+ version?: string;
120
+ /** Android API Level。 */
121
+ sdkInt?: number;
122
+ /** 其他平台特有字段。 */
123
+ [key: string]: unknown;
124
+ }
125
+ /** 权限状态。 */
126
+ export type PermissionState = "granted" | "denied" | "prompt" | "restricted";
127
+ /** 权限描述(permissions.check 返回值)。 */
128
+ export interface PermissionStatus {
129
+ /** 权限名。 */
130
+ permission: string;
131
+ /** 状态。 */
132
+ state: PermissionState;
133
+ }
134
+ /**
135
+ * HTTP 响应(`http.fetch` / `http.get` 等返回)。
136
+ * 对应后端 `HttpPlugin.HttpResponseResult`。
137
+ */
138
+ export interface HttpResponse {
139
+ /** HTTP 状态码。 */
140
+ statusCode: number;
141
+ /** 是否为成功状态(2xx)。 */
142
+ ok: boolean;
143
+ /** 响应体文本。后端统一以字符串返回,JSON 需自行 `JSON.parse`。 */
144
+ body: string;
145
+ /** 响应内容类型。 */
146
+ contentType: string;
147
+ /** 响应头。 */
148
+ headers: Record<string, string>;
149
+ }
150
+ /** `http.fetch` 的请求选项。对应后端 `HttpPlugin.HttpRequestOptions`。 */
151
+ export interface HttpRequestOptions {
152
+ /** 请求 URL(必填)。 */
153
+ url: string;
154
+ /** HTTP 方法,默认 `GET`。 */
155
+ method?: string;
156
+ /** 请求体。 */
157
+ body?: string;
158
+ /** 请求内容类型。 */
159
+ contentType?: string;
160
+ /** 附加请求头。 */
161
+ headers?: Record<string, string>;
162
+ }
163
+ /** 上传 / 下载进度回调参数。 */
164
+ export interface TransferProgress {
165
+ /** 已传输字节。 */
166
+ transferred: number;
167
+ /** 总字节(未知时为 -1)。 */
168
+ total: number;
169
+ /** 进度比例 0..1。 */
170
+ percent: number;
171
+ }
172
+ /**
173
+ * 屏幕信息(`screen.getAll` / `screen.getPrimary` 返回值)。
174
+ * 对应后端 `Wails.Net.Application.Screens.Screen`,字段与其一一对应。
175
+ */
176
+ export interface ScreenInfo {
177
+ /** 屏幕 ID。 */
178
+ id: string;
179
+ /** 屏幕名称。 */
180
+ name: string;
181
+ /** 逻辑坐标 X。 */
182
+ x: number;
183
+ /** 逻辑坐标 Y。 */
184
+ y: number;
185
+ /** 逻辑宽。 */
186
+ width: number;
187
+ /** 逻辑高。 */
188
+ height: number;
189
+ /** 工作区(不含任务栏)逻辑坐标 X。 */
190
+ workAreaX: number;
191
+ /** 工作区逻辑坐标 Y。 */
192
+ workAreaY: number;
193
+ /** 工作区逻辑宽。 */
194
+ workAreaWidth: number;
195
+ /** 工作区逻辑高。 */
196
+ workAreaHeight: number;
197
+ /** 物理像素坐标 X。 */
198
+ physicalX: number;
199
+ /** 物理像素坐标 Y。 */
200
+ physicalY: number;
201
+ /** 物理像素宽。 */
202
+ physicalWidth: number;
203
+ /** 物理像素高。 */
204
+ physicalHeight: number;
205
+ /** 物理工作区坐标 X。 */
206
+ physicalWorkAreaX: number;
207
+ /** 物理工作区坐标 Y。 */
208
+ physicalWorkAreaY: number;
209
+ /** 物理工作区宽。 */
210
+ physicalWorkAreaWidth: number;
211
+ /** 物理工作区高。 */
212
+ physicalWorkAreaHeight: number;
213
+ /** 缩放因子(DPI)。 */
214
+ scaleFactor: number;
215
+ /** 是否为主屏。 */
216
+ isPrimary: boolean;
217
+ /** 旋转角度(度)。 */
218
+ rotation: number;
219
+ /** 缩略图(Base64)。 */
220
+ thumbnail?: string;
221
+ }
222
+ /** 窗口位置(左上角坐标)。 */
223
+ export interface Point {
224
+ x: number;
225
+ y: number;
226
+ }
227
+ /** 窗口尺寸。 */
228
+ export interface Size {
229
+ width: number;
230
+ height: number;
231
+ }
232
+ /** 窗口信息(windows.getAll 等返回)。 */
233
+ export interface WindowInfo {
234
+ /** 窗口 ID。 */
235
+ id: number;
236
+ /** 窗口名称。 */
237
+ name: string;
238
+ }
239
+ /**
240
+ * 菜单项预定义角色,对应后端 `Wails.Net.Application.Menus.MenuRole` 枚举。
241
+ *
242
+ * 后端命令层已注册 `JsonStringEnumConverter(JsonNamingPolicy.CamelCase)`,
243
+ * 因此这里直接使用驼峰字符串即可(无需传枚举序号)。
244
+ * macOS 专属角色在 Windows/Linux 上静默降级为 no-op。
245
+ */
246
+ export type MenuRole = "none" | "separator" | "copy" | "cut" | "paste" | "selectAll" | "undo" | "redo" | "minimize" | "maximize" | "fullscreen" | "closeWindow" | "zoom" | "about" | "quit" | "hide" | "hideOthers" | "showAll" | "services" | "bringAllToFront" | "toggleFullScreen";
247
+ /**
248
+ * 菜单节点。对应后端 `Wails.Net.Application.Menus.Menu`。
249
+ *
250
+ * 后端的 `MenuItem` 继承自 `Menu`,因此菜单是**递归的单一节点结构**
251
+ * (而非「菜单 = 菜单项数组」):顶层 `Menu` 通过 `items` 挂载子项,
252
+ * 子项自身又可通过 `isSubMenu` + `items` 继续嵌套。
253
+ */
254
+ export interface Menu {
255
+ /** 显示文本。 */
256
+ label?: string;
257
+ /** 图标位图(Base64 字符串,对应后端 `byte[]`)。 */
258
+ bitmap?: string;
259
+ /** 是否为子菜单容器。 */
260
+ isSubMenu?: boolean;
261
+ /** 是否为复选框项。 */
262
+ isCheckbox?: boolean;
263
+ /** 是否为单选项。 */
264
+ isRadio?: boolean;
265
+ /** 是否为分隔符。 */
266
+ isSeparator?: boolean;
267
+ /** 是否禁用。 */
268
+ isDisabled?: boolean;
269
+ /** 是否处于勾选状态。 */
270
+ checked?: boolean;
271
+ /** 快捷键(如 `"Ctrl+C"`)。 */
272
+ accelerator?: string;
273
+ /** 子项列表。 */
274
+ items?: MenuItem[];
275
+ }
276
+ /**
277
+ * 菜单项。对应后端 `Wails.Net.Application.Menus.MenuItem`(继承 `Menu`)。
278
+ *
279
+ * 注意:后端的 `Callback` / `CallbackWithContext` 是服务端委托,
280
+ * 不参与 JSON 传输,故此处不暴露。前端响应点击请监听菜单事件。
281
+ */
282
+ export interface MenuItem extends Menu {
283
+ /** 菜单项 ID(后端 `uint`,用于 `menu.updateMenuItem` 等定位)。 */
284
+ id?: number;
285
+ /** 预定义角色。设置后平台将调用系统原生命令,忽略自定义回调。 */
286
+ role?: MenuRole;
287
+ /** "关于" 对话框元数据(仅 `role: "about"` 时有意义)。 */
288
+ aboutMetadata?: AboutMetadata;
289
+ }
290
+ /** "关于" 对话框元数据。对应后端 `Wails.Net.Application.Menus.AboutMetadata`。 */
291
+ export interface AboutMetadata {
292
+ /** 应用名称。 */
293
+ name?: string;
294
+ /** 完整版本号。 */
295
+ version?: string;
296
+ /** 简短版本号。 */
297
+ shortVersion?: string;
298
+ /** 作者。 */
299
+ authors?: string;
300
+ /** 版权信息。 */
301
+ copyright?: string;
302
+ /** 许可证。 */
303
+ license?: string;
304
+ /** 官网 URL。 */
305
+ website?: string;
306
+ /** 官网显示文本。 */
307
+ websiteLabel?: string;
308
+ /** 说明文本。 */
309
+ comments?: string;
310
+ }
311
+ /** CLI 选项值(对应后端 `CliArgValue`)。 */
312
+ export interface CliArgValue {
313
+ /** 是否存在(flag 类选项)。 */
314
+ exists: boolean;
315
+ /** 字符串值(单值选项)。 */
316
+ value?: string;
317
+ /** 多值列表(多值选项)。 */
318
+ values?: string[];
319
+ }
320
+ /** CLI 解析结果(对应后端 `CliMatches`,`cli.getMatches` 返回)。 */
321
+ export interface CliMatches {
322
+ /** 非选项参数数组(位置参数)。 */
323
+ args: string[];
324
+ /** 选项字典,按参数长名索引。 */
325
+ options: Record<string, CliArgValue>;
326
+ /** 匹配的子命令名;未匹配子命令时为 null。 */
327
+ subcommand?: string;
328
+ }
329
+ /** Shell 命令执行结果(对应后端 `ShellPlugin.ShellResult`)。 */
330
+ export interface ShellResult {
331
+ /** 退出码。 */
332
+ exitCode: number;
333
+ /** 标准输出。 */
334
+ stdout: string;
335
+ /** 标准错误。 */
336
+ stderr: string;
337
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 公共选项 / 数据结构类型。
3
+ *
4
+ * 这些类型与 Wails.Net 后端插件命令的参数 / 返回值对应。字段命名采用 camelCase
5
+ * (与后端 `JsonSerializerDefaults.Web` + camelCase 输出一致)。`byte[]` 在前端以
6
+ * Base64 字符串表示,`Guid` 以字符串,`DateTime` 以 ISO-8601 字符串。
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/api/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Wails.Net 双向通道 API(RH-IPC-05,对齐 Tauri v2 Channel / Wails v3 Channel 原语)。
3
+ *
4
+ * 通道弥补请求-响应模型的局限:后端命令可通过 `ICommandContext.Channel` 主动向前端
5
+ * 推送流式消息(长耗时任务进度回传),前端也可通过同一通道向命令回送消息。
6
+ *
7
+ * 用法:
8
+ * ```ts
9
+ * import { createChannel, wails } from "@wailsnet/runtime";
10
+ *
11
+ * const ch = await createChannel();
12
+ * ch.onMessage((progress) => console.log("progress:", progress));
13
+ * await wails.call("Updater.Install", [args], { channel: ch.id });
14
+ * ch.send({ status: "ready" }); // 前端 → 命令
15
+ * await ch.close(); // 结束
16
+ * ```
17
+ *
18
+ * 协议(与后端 `ChannelProtocol` 对齐):
19
+ * - 上行 `channel:open` / `channel:message` / `channel:close`;
20
+ * - 下行复用事件通道:保留事件名 `wails:channel:<id>`,由 transport 在用户事件回调前拦截。
21
+ */
22
+ import { type CancellablePromise } from "../internal/transport.js";
23
+ /** 双向通道句柄。 */
24
+ export interface WailsChannel {
25
+ /** 通道唯一标识(前端未指定时由后端生成)。 */
26
+ readonly id: string;
27
+ /**
28
+ * 订阅后端下发的消息,返回取消订阅函数。
29
+ * @param callback 载荷回调;`event` 为可选事件标识(通常为 null)。
30
+ */
31
+ onMessage(callback: (payload: unknown, event: string | null) => void): () => void;
32
+ /** 向后端命令发送消息(fire-and-forget)。 */
33
+ send(payload: unknown, event?: string | null): void;
34
+ /** 关闭通道(后端会下发关闭信号,之后 send 抛错)。 */
35
+ close(): CancellablePromise<unknown>;
36
+ }
37
+ /** 创建通道的选项。 */
38
+ export interface CreateChannelOptions {
39
+ /** 指定通道 ID;缺省由后端生成。 */
40
+ id?: string;
41
+ }
42
+ /** 已关闭通道上执行 send 时的错误。 */
43
+ export declare class ChannelClosedError extends Error {
44
+ constructor(id: string);
45
+ }
46
+ /**
47
+ * 创建一个与后端绑定的双向通道。
48
+ * @param options 可选;`id` 指定通道 ID(重复 ID 幂等:返回同一通道语义)。
49
+ */
50
+ export declare function createChannel(options?: CreateChannelOptions): Promise<WailsChannel>;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Wails.Net 双向通道 API(RH-IPC-05,对齐 Tauri v2 Channel / Wails v3 Channel 原语)。
3
+ *
4
+ * 通道弥补请求-响应模型的局限:后端命令可通过 `ICommandContext.Channel` 主动向前端
5
+ * 推送流式消息(长耗时任务进度回传),前端也可通过同一通道向命令回送消息。
6
+ *
7
+ * 用法:
8
+ * ```ts
9
+ * import { createChannel, wails } from "@wailsnet/runtime";
10
+ *
11
+ * const ch = await createChannel();
12
+ * ch.onMessage((progress) => console.log("progress:", progress));
13
+ * await wails.call("Updater.Install", [args], { channel: ch.id });
14
+ * ch.send({ status: "ready" }); // 前端 → 命令
15
+ * await ch.close(); // 结束
16
+ * ```
17
+ *
18
+ * 协议(与后端 `ChannelProtocol` 对齐):
19
+ * - 上行 `channel:open` / `channel:message` / `channel:close`;
20
+ * - 下行复用事件通道:保留事件名 `wails:channel:<id>`,由 transport 在用户事件回调前拦截。
21
+ */
22
+ import { transport } from "../internal/transport.js";
23
+ /** 已关闭通道上执行 send 时的错误。 */
24
+ export class ChannelClosedError extends Error {
25
+ constructor(id) {
26
+ super(`channel "${id}" 已关闭`);
27
+ this.name = "ChannelClosedError";
28
+ }
29
+ }
30
+ /** 内部实现:带关闭态的通道。 */
31
+ class ChannelImpl {
32
+ constructor(id) {
33
+ Object.defineProperty(this, "id", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "_closed", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: false
44
+ });
45
+ this.id = id;
46
+ // 构造期即订阅关闭信号:即使用户从未调用 onMessage,
47
+ // 后端下发的 closed 也能正确标记关闭态(send 抛 ChannelClosedError)。
48
+ transport.onChannelMessage(id, (_payload, _event, closed) => {
49
+ if (closed)
50
+ this._closed = true;
51
+ });
52
+ }
53
+ get isClosed() {
54
+ return this._closed;
55
+ }
56
+ onMessage(callback) {
57
+ return transport.onChannelMessage(this.id, (payload, event, closed) => {
58
+ // 关闭信号由构造期订阅负责,这里只派发业务消息。
59
+ if (closed)
60
+ return;
61
+ callback(payload, event);
62
+ });
63
+ }
64
+ send(payload, event) {
65
+ if (this._closed)
66
+ throw new ChannelClosedError(this.id);
67
+ transport.send("channel:message", { channelId: this.id, event: event ?? null, data: payload });
68
+ }
69
+ close() {
70
+ this._closed = true;
71
+ return transport.invoke("channel:close", { channelId: this.id });
72
+ }
73
+ }
74
+ /**
75
+ * 创建一个与后端绑定的双向通道。
76
+ * @param options 可选;`id` 指定通道 ID(重复 ID 幂等:返回同一通道语义)。
77
+ */
78
+ export async function createChannel(options) {
79
+ const result = await transport.invoke("channel:open", options?.id != null ? { id: options.id } : {});
80
+ const id = result?.channelId;
81
+ if (typeof id !== "string" || id.length === 0) {
82
+ throw new Error("channel:open 未返回 channelId");
83
+ }
84
+ return new ChannelImpl(id);
85
+ }
86
+ //# sourceMappingURL=channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.js","sourceRoot":"","sources":["../../src/core/channel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,SAAS,EAA2B,MAAM,0BAA0B,CAAC;AAuB9E,0BAA0B;AAC1B,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,EAAU;QACpB,KAAK,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,oBAAoB;AACpB,MAAM,WAAW;IAIf,YAAY,EAAU;QAHb;;;;;WAAW;QACZ;;;;mBAAU,KAAK;WAAC;QAGtB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,iCAAiC;QACjC,qDAAqD;QACrD,SAAS,CAAC,gBAAgB,CAAC,EAAE,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YAC1D,IAAI,MAAM;gBAAE,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,SAAS,CAAC,QAA0D;QAClE,OAAO,SAAS,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YACpE,0BAA0B;YAC1B,IAAI,MAAM;gBAAE,OAAO;YACnB,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC,OAAgB,EAAE,KAAqB;QAC1C,IAAI,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACxD,SAAS,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,IAAI,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IACjG,CAAC;IAED,KAAK;QACH,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,SAAS,CAAC,MAAM,CAAC,eAAe,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,OAA8B;IAChE,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CACnC,cAAc,EACd,OAAO,EAAE,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAC9C,CAAC;IACF,MAAM,EAAE,GAAG,MAAM,EAAE,SAAS,CAAC;IAC7B,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,34 @@
1
+ import type { CancellablePromise } from "../internal/transport.js";
2
+ /** 参数打包模式,对应线协议约定。 */
3
+ export type PackMode =
4
+ /** 无参数 → 发送 `[]`(后端传 default)。 */
5
+ "none"
6
+ /** 单业务参数 → 包装为 `[{...}]`(后端按唯一参数整体反序列化)。 */
7
+ | "single"
8
+ /** 多位置参数 → 原样展开 `[...]`(后端按位置逐个反序列化)。 */
9
+ | "spread";
10
+ /**
11
+ * 定义一条类型化命令。
12
+ *
13
+ * @typeParam A 参数元组(`[arg1, arg2]`),决定调用函数的参数约束。
14
+ * @typeParam R 返回类型,决定 `CancellablePromise<R>` 的推导。
15
+ * @param name 完整命令名(`ns.method`,与后端 MapCommand 注册名一致)。
16
+ * @param pack 参数打包模式(none / single / spread)。
17
+ * @returns 类型化调用函数:`(...args: A) => CancellablePromise<R>`。
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // 单参数对象:自动包装为 [{ channel }]
22
+ * export const checkForUpdate = defineCommand<[UpdateChannel], UpdateManifest>(
23
+ * "updater.checkForUpdate", "single");
24
+ * await checkForUpdate("stable"); // ^? CancellablePromise<UpdateManifest>
25
+ *
26
+ * // 多位置参数:原样展开
27
+ * export const setSize = defineCommand<[number, number], void>("window.setSize", "spread");
28
+ * setSize(800, 600);
29
+ *
30
+ * // 无参数
31
+ * export const getCurrentVersion = defineCommand<[], string>("updater.getCurrentVersion", "none");
32
+ * ```
33
+ */
34
+ export declare function defineCommand<A extends unknown[], R>(name: string, pack: PackMode): (...args: A) => CancellablePromise<R>;