@dimina/desktop-sdk 0.0.0-stage → 1.9.0-beta.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.
- package/LICENSE +202 -0
- package/README.md +24 -2
- package/index.d.ts +21 -0
- package/index.js +158 -0
- package/package.json +54 -4
- package/tauri.d.ts +14 -0
- package/tauri.js +28 -0
- package/types.d.ts +14 -0
- package/window.js +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright (C) 2017 Beijing Didi Infinity Technology and Development Co.,Ltd. All rights reserved.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dimina/desktop-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
供 Node.js / Electron 主进程和 Tauri 前端调用的 Dimina 独立窗口容器 SDK。Node/Electron 入口零 npm 运行时依赖,使用配套的桌面运行时;不会自动下载或安装运行时。
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import { Dimina } from '@dimina/desktop-sdk'
|
|
7
|
+
|
|
8
|
+
const desktop = await Dimina.start({ runtimePath, packagesDir, dataDir })
|
|
9
|
+
const window = await desktop.openApp({ appId: 'your-app-id', name: '我的小程序' })
|
|
10
|
+
await window.focus()
|
|
11
|
+
await window.close()
|
|
12
|
+
await desktop.dispose()
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
三个目录/文件路径都必须是绝对路径。`runtimePath` 在 macOS 指向完整 `.app` 内的可执行文件;Windows 指向配套 `.exe`;Linux 指向配套可执行文件。`packagesDir` 包含按 appId 命名的 Dimina 编译产物目录,`dataDir` 由当前产品独占。
|
|
16
|
+
|
|
17
|
+
还提供 `windows()`、`closeAll()`、`importApp(absolutePath)`。重复打开同 appId 会复用已有窗口;首次打开可传 `path` 指定入口。客户端退出时等待 `dispose()`,不要把 SDK 实例暴露给 renderer 或小程序。
|
|
18
|
+
|
|
19
|
+
主进程可用 `await desktop.registerApi(name, handler, { appIds, timeoutMs })` 注册原生能力,为移动 SDK 已有 API 提供桌面原生实现。处理器接收 `(params, { appId, windowId, signal })`,返回 JSON 对象或 Promise;异步操作可使用 `signal` 参与取消。
|
|
20
|
+
|
|
21
|
+
完整构建、平台依赖和分发说明见仓库 `desktop/sdk/README.md`。
|
|
22
|
+
|
|
23
|
+
Tauri 2 前端使用 `import { Dimina } from '@dimina/desktop-sdk/tauri'`,注册配套 Rust 插件和权限后调用 `await Dimina.start()`。运行时路径由 Rust 宿主配置,完整接入步骤见 `desktop/sdk/tauri/README.md`。
|
|
24
|
+
|
|
25
|
+
源码开发时运行 `pnpm test` 需要 Node.js 22+ 和 Go 1.27.3+,测试会编译跨平台的原生进程夹具;Go 可通过 `GO_BIN` 指定。CI 使用 `DIMINA_TEST_RUNTIME` 指向预编译夹具。业务集成不需要安装这些测试工具。
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface DesktopOptions {
|
|
2
|
+
runtimePath: string
|
|
3
|
+
packagesDir: string
|
|
4
|
+
dataDir: string
|
|
5
|
+
startupTimeoutMs?: number
|
|
6
|
+
onLog?: (message: string) => void
|
|
7
|
+
}
|
|
8
|
+
import type { OpenAppOptions, ImportedApp, DiminaWindow, NativeApiHandler, NativeApiOptions } from './types.js'
|
|
9
|
+
export * from './types.js'
|
|
10
|
+
export declare class Dimina {
|
|
11
|
+
private constructor()
|
|
12
|
+
static start(options: DesktopOptions): Promise<Dimina>
|
|
13
|
+
registerApi(name: string, handler: NativeApiHandler, options?: NativeApiOptions): Promise<void>
|
|
14
|
+
openApp(options: OpenAppOptions): Promise<DiminaWindow>
|
|
15
|
+
focus(id: string): Promise<void>
|
|
16
|
+
closeApp(id: string): Promise<void>
|
|
17
|
+
closeAll(): Promise<void>
|
|
18
|
+
windows(): Promise<DiminaWindow[]>
|
|
19
|
+
importApp(path: string): Promise<ImportedApp>
|
|
20
|
+
dispose(): Promise<void>
|
|
21
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { createInterface } from 'node:readline'
|
|
3
|
+
import { isAbsolute } from 'node:path'
|
|
4
|
+
|
|
5
|
+
import { DiminaWindow } from './window.js'
|
|
6
|
+
export { DiminaWindow } from './window.js'
|
|
7
|
+
|
|
8
|
+
/** Call from Node.js or Electron's main process, never expose the host to a renderer. */
|
|
9
|
+
export class Dimina {
|
|
10
|
+
#process
|
|
11
|
+
#base
|
|
12
|
+
#token
|
|
13
|
+
#stopped = false
|
|
14
|
+
#exit
|
|
15
|
+
#disposal
|
|
16
|
+
#apis = new Map()
|
|
17
|
+
#calls = new Map()
|
|
18
|
+
|
|
19
|
+
static async start(options) {
|
|
20
|
+
for (const name of ['runtimePath', 'packagesDir', 'dataDir']) {
|
|
21
|
+
if (!isAbsolute(options[name] || '')) throw new Error(`${name} must be an absolute path`)
|
|
22
|
+
}
|
|
23
|
+
const child = spawn(options.runtimePath, ['--sdk-host', '--packages-dir', options.packagesDir, '--data-dir', options.dataDir], {
|
|
24
|
+
stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true,
|
|
25
|
+
})
|
|
26
|
+
const host = new Dimina()
|
|
27
|
+
host.#process = child
|
|
28
|
+
child.stdin.on('error', () => {})
|
|
29
|
+
host.#exit = new Promise(resolve => child.once('exit', resolve))
|
|
30
|
+
child.once('exit', () => {
|
|
31
|
+
host.#stopped = true
|
|
32
|
+
for (const controller of host.#calls.values()) controller.abort()
|
|
33
|
+
host.#calls.clear()
|
|
34
|
+
})
|
|
35
|
+
let diagnostics = ''
|
|
36
|
+
child.stderr.on('data', bytes => {
|
|
37
|
+
diagnostics = (diagnostics + bytes).slice(-4096)
|
|
38
|
+
try { options.onLog?.(String(bytes)) } catch { /* Logging is an optional host hook. */ }
|
|
39
|
+
})
|
|
40
|
+
const lines = createInterface({ input: child.stdout })
|
|
41
|
+
try {
|
|
42
|
+
const hello = await new Promise((resolve, reject) => {
|
|
43
|
+
const timer = setTimeout(() => reject(new Error('Dimina runtime startup timed out')), options.startupTimeoutMs ?? 30000)
|
|
44
|
+
const error = value => { clearTimeout(timer); reject(value) }
|
|
45
|
+
child.once('error', error)
|
|
46
|
+
child.once('exit', code => error(new Error(`Dimina runtime exited (${code}): ${diagnostics}`)))
|
|
47
|
+
lines.once('line', line => {
|
|
48
|
+
clearTimeout(timer)
|
|
49
|
+
try { resolve(JSON.parse(line)) } catch { reject(new Error('Invalid Dimina runtime handshake')) }
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
const base = new URL(hello.baseUrl)
|
|
53
|
+
if (hello.protocol !== 1 || base.protocol !== 'http:' || base.hostname !== '127.0.0.1'
|
|
54
|
+
|| !base.pathname.endsWith('/sdk/control/') || !/^[a-f0-9]{48}$/.test(hello.token)) {
|
|
55
|
+
throw new Error('Unsupported Dimina runtime handshake')
|
|
56
|
+
}
|
|
57
|
+
host.#base = base
|
|
58
|
+
host.#token = hello.token
|
|
59
|
+
lines.on('line', line => {
|
|
60
|
+
try { host.#native(JSON.parse(line)) } catch { /* Ignore non-protocol output. */ }
|
|
61
|
+
})
|
|
62
|
+
child.once('exit', () => lines.close())
|
|
63
|
+
return host
|
|
64
|
+
} catch (error) {
|
|
65
|
+
child.stdin.end()
|
|
66
|
+
child.kill('SIGKILL')
|
|
67
|
+
lines.close()
|
|
68
|
+
child.stdout.resume()
|
|
69
|
+
throw error
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
#native(message) {
|
|
74
|
+
if (message.type === 'native-cancel') {
|
|
75
|
+
this.#calls.get(message.id)?.abort()
|
|
76
|
+
this.#calls.delete(message.id)
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
if (message.type !== 'native-call' || this.#stopped) return
|
|
80
|
+
const controller = new AbortController()
|
|
81
|
+
this.#calls.set(message.id, controller)
|
|
82
|
+
const context = { appId: message.appId, windowId: message.windowId, signal: controller.signal }
|
|
83
|
+
const respond = response => {
|
|
84
|
+
if (!controller.signal.aborted && !this.#stopped) {
|
|
85
|
+
let encoded = JSON.stringify({ type: 'native-result', id: message.id, ...response })
|
|
86
|
+
if (Buffer.byteLength(encoded) > 1 << 20) encoded = JSON.stringify({ type: 'native-result', id: message.id, error: 'Native API result exceeds 1 MiB' })
|
|
87
|
+
this.#process.stdin.write(encoded + '\n')
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
Promise.resolve().then(() => {
|
|
91
|
+
if (controller.signal.aborted || this.#stopped) return
|
|
92
|
+
const handler = this.#apis.get(message.name)
|
|
93
|
+
if (!handler) throw new Error('Native API handler is unavailable')
|
|
94
|
+
return handler(message.params, context)
|
|
95
|
+
}).then(value => {
|
|
96
|
+
if (value != null && (typeof value !== 'object' || Array.isArray(value))) throw new Error('Native API must return a JSON object')
|
|
97
|
+
respond({ value: value ?? {} })
|
|
98
|
+
}).catch(error => {
|
|
99
|
+
try { respond({ error: error?.message || String(error) }) } catch { /* The host may already be exiting. */ }
|
|
100
|
+
}).finally(() => this.#calls.delete(message.id))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async registerApi(name, handler, options = {}) {
|
|
104
|
+
if (typeof handler !== 'function') throw new TypeError('Native API handler must be a function')
|
|
105
|
+
const previous = this.#apis.get(name)
|
|
106
|
+
this.#apis.set(name, handler)
|
|
107
|
+
try { await this.#call('registerApi', { name, appIds: options.appIds, timeoutMs: options.timeoutMs }) }
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (this.#apis.get(name) === handler) {
|
|
110
|
+
if (previous) this.#apis.set(name, previous)
|
|
111
|
+
else this.#apis.delete(name)
|
|
112
|
+
}
|
|
113
|
+
throw error
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async #call(method, payload, verb = 'POST') {
|
|
118
|
+
if (this.#stopped || this.#disposal) throw new Error('Dimina runtime is stopped')
|
|
119
|
+
return this.#request(method, payload, verb)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async #request(method, payload, verb = 'POST', signal = AbortSignal.timeout(100000)) {
|
|
123
|
+
const response = await fetch(new URL(method, this.#base), {
|
|
124
|
+
method: verb,
|
|
125
|
+
headers: { Authorization: `Bearer ${this.#token}`, 'Content-Type': 'application/json' },
|
|
126
|
+
body: verb === 'GET' ? undefined : JSON.stringify(payload ?? {}),
|
|
127
|
+
signal,
|
|
128
|
+
})
|
|
129
|
+
let result
|
|
130
|
+
try { result = await response.json() } catch { throw new Error(`Invalid runtime response (HTTP ${response.status})`) }
|
|
131
|
+
if (!response.ok || result.error) throw new Error(result.error || `Runtime HTTP ${response.status}`)
|
|
132
|
+
return result.value
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async openApp(options) { return new DiminaWindow(this, await this.#call('open', options)) }
|
|
136
|
+
focus(id) { return this.#call('focus', { id }) }
|
|
137
|
+
closeApp(id) { return this.#call('close', { id }) }
|
|
138
|
+
closeAll() { return this.#call('closeAll') }
|
|
139
|
+
async windows() { return (await this.#call('windows', undefined, 'GET')).map(info => new DiminaWindow(this, info)) }
|
|
140
|
+
importApp(path) { return this.#call('import', { path }) }
|
|
141
|
+
|
|
142
|
+
dispose() {
|
|
143
|
+
return this.#disposal ??= this.#disposeRuntime()
|
|
144
|
+
}
|
|
145
|
+
async #disposeRuntime() {
|
|
146
|
+
if (this.#stopped) return
|
|
147
|
+
try { await this.#request('closeAll', undefined, 'POST', AbortSignal.timeout(3000)) } finally {
|
|
148
|
+
this.#stopped = true
|
|
149
|
+
for (const controller of this.#calls.values()) controller.abort()
|
|
150
|
+
this.#calls.clear()
|
|
151
|
+
this.#process.stdin.end()
|
|
152
|
+
const timer = setTimeout(() => this.#process.kill('SIGKILL'), 3000)
|
|
153
|
+
try { await this.#exit } finally { clearTimeout(timer) }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async [Symbol.asyncDispose]() { await this.dispose() }
|
|
157
|
+
}
|
|
158
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dimina/desktop-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.9.0-beta.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Standalone Dimina mini-program windows for Node.js, Electron and Tauri hosts",
|
|
6
|
+
"homepage": "https://github.com/didi/dimina/tree/main/desktop/sdk",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/didi/dimina.git",
|
|
10
|
+
"directory": "desktop/sdk/node"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/didi/dimina/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"registry": "https://registry.npmjs.org"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./index.js",
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"default": "./index.js"
|
|
26
|
+
},
|
|
27
|
+
"./tauri": {
|
|
28
|
+
"types": "./tauri.d.ts",
|
|
29
|
+
"default": "./tauri.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"*.js",
|
|
37
|
+
"*.d.ts",
|
|
38
|
+
"README.md"
|
|
39
|
+
],
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@tauri-apps/api": "^2.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"@tauri-apps/api": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@tauri-apps/api": "^2.0.0",
|
|
50
|
+
"typescript": "^5.9.3"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"test": "node --test test/*.test.mjs",
|
|
54
|
+
"typecheck": "tsc --noEmit --strict --module NodeNext --moduleResolution NodeNext --target ES2022 test/types.ts"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/tauri.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OpenAppOptions, ImportedApp, DiminaWindow } from './types.js'
|
|
2
|
+
export * from './types.js'
|
|
3
|
+
/** One runtime is shared by the trusted frontend windows of a Tauri app. */
|
|
4
|
+
export declare class Dimina {
|
|
5
|
+
private constructor()
|
|
6
|
+
static start(): Promise<Dimina>
|
|
7
|
+
openApp(options: OpenAppOptions): Promise<DiminaWindow>
|
|
8
|
+
focus(id: string): Promise<void>
|
|
9
|
+
closeApp(id: string): Promise<void>
|
|
10
|
+
closeAll(): Promise<void>
|
|
11
|
+
windows(): Promise<DiminaWindow[]>
|
|
12
|
+
importApp(path: string): Promise<ImportedApp>
|
|
13
|
+
dispose(): Promise<void>
|
|
14
|
+
}
|
package/tauri.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { invoke } from '@tauri-apps/api/core'
|
|
2
|
+
import { DiminaWindow } from './window.js'
|
|
3
|
+
export { DiminaWindow } from './window.js'
|
|
4
|
+
|
|
5
|
+
/** Uses the runtime configured by the host's tauri-plugin-dimina instance. */
|
|
6
|
+
export class Dimina {
|
|
7
|
+
#stopped = false
|
|
8
|
+
|
|
9
|
+
static async start() {
|
|
10
|
+
await invoke('plugin:dimina|start')
|
|
11
|
+
return new Dimina()
|
|
12
|
+
}
|
|
13
|
+
#call(method, args) {
|
|
14
|
+
if (this.#stopped) return Promise.reject(new Error('Dimina runtime is stopped'))
|
|
15
|
+
return invoke(`plugin:dimina|${method}`, args)
|
|
16
|
+
}
|
|
17
|
+
async openApp(options) { return new DiminaWindow(this, await this.#call('open', { options })) }
|
|
18
|
+
focus(id) { return this.#call('focus', { id }) }
|
|
19
|
+
closeApp(id) { return this.#call('close', { id }) }
|
|
20
|
+
closeAll() { return this.#call('close_all') }
|
|
21
|
+
async windows() { return (await this.#call('windows')).map(info => new DiminaWindow(this, info)) }
|
|
22
|
+
importApp(path) { return this.#call('import', { path }) }
|
|
23
|
+
async dispose() {
|
|
24
|
+
if (this.#stopped) return
|
|
25
|
+
await this.#call('dispose')
|
|
26
|
+
this.#stopped = true
|
|
27
|
+
}
|
|
28
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface OpenAppOptions { appId: string; path?: string; name?: string }
|
|
2
|
+
export interface ImportedApp { appId: string; name: string; path: string; imported: boolean }
|
|
3
|
+
export interface NativeApiOptions { appIds?: string[]; timeoutMs?: number }
|
|
4
|
+
export interface NativeApiContext { appId: string; windowId: string; signal: AbortSignal }
|
|
5
|
+
export type NativeApiHandler = (params: Record<string, unknown>, context: NativeApiContext) => Record<string, unknown> | void | Promise<Record<string, unknown> | void>
|
|
6
|
+
export declare class DiminaWindow {
|
|
7
|
+
private constructor()
|
|
8
|
+
readonly id: string
|
|
9
|
+
readonly appId: string
|
|
10
|
+
readonly name: string
|
|
11
|
+
readonly ready: boolean
|
|
12
|
+
focus(): Promise<void>
|
|
13
|
+
close(): Promise<void>
|
|
14
|
+
}
|
package/window.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class DiminaWindow {
|
|
2
|
+
constructor(host, info) {
|
|
3
|
+
this.host = host
|
|
4
|
+
this.id = info.id
|
|
5
|
+
this.appId = info.appId
|
|
6
|
+
this.name = info.name
|
|
7
|
+
this.ready = info.ready
|
|
8
|
+
}
|
|
9
|
+
focus() { return this.host.focus(this.id) }
|
|
10
|
+
close() { return this.host.closeApp(this.id) }
|
|
11
|
+
}
|