@orielweb/api 0.0.0-stage → 0.4.14

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/src/opener.js ADDED
@@ -0,0 +1,74 @@
1
+ // 对标 `@tauri-apps/plugin-opener`。
2
+ //
3
+ // 三个函数的共同点:**它们在外面的世界里产生副作用**(拉起浏览器、用默认程序打开文件、
4
+ // 弹文件管理器)。所以它们都**需要能力声明**,不像 `path.*` 那样整族放行:
5
+ //
6
+ // c.Allow("opener.openUrl") // 能拉起任意 URL(含自定义协议处理器)
7
+ // c.Allow("opener.openPath") // 能用默认程序打开任意文件——双击一个 .exe 就是执行它
8
+ //
9
+ // 与 Tauri 的差别:**不支持 `openWith`(用指定程序打开)**,传了会明确报错而不是
10
+ // 静默降级成"用默认程序打开"。理由见宿主侧 OrielBuiltInPlatformCommands 的说明:
11
+ // 那个参数等于任意程序执行,需要一套"允许哪些程序"的授权模型。
12
+
13
+ import { invoke } from './core.js';
14
+
15
+ /**
16
+ * 用系统默认方式打开一个 URL(通常是浏览器)。
17
+ *
18
+ * @param {string} url **绝对 URL**(含协议)。相对 URL 会被拒——系统不知道该拿它怎么办。
19
+ * @param {string} [openWith] **不支持**,传了会抛错。
20
+ * @returns {Promise<boolean>} 是否成功拉起。
21
+ * @throws {Error} `url` 不是绝对 URL、或传了 `openWith` 时抛出(`code` 分别是
22
+ * `invalid_argument` / `not_supported`)。
23
+ * @remarks
24
+ * **宿主还会按协议白名单过滤一次**(`OrielShellOptions.AllowedSchemes`,
25
+ * 默认只放 `http` / `https` / `mailto`):`file:`、`javascript:`、裸路径与含控制字符的目标
26
+ * 会被拒绝并返回 `false`(不抛错——"这个协议不在白名单里"是配置决定,不是调用错误)。
27
+ * 要放行自定义协议(如 `myapp:`)请在宿主侧配置白名单,而不是在页面侧拼字符串绕过。
28
+ */
29
+ export function openUrl(url, openWith) {
30
+ return invoke('opener.openUrl', openWith === undefined ? { url } : { url, with: openWith });
31
+ }
32
+
33
+ /**
34
+ * 用系统默认程序打开一个文件或目录。
35
+ *
36
+ * @param {string} path
37
+ * @param {string} [openWith] **不支持**,传了会抛错。
38
+ * @returns {Promise<void>}
39
+ * @remarks
40
+ * **这条命令的授权等于"可以用默认程序打开任意文件"**。对一个可执行文件来说,
41
+ * 那就是"执行它"——所以它的 scope 就是"应用信任自己的页面到这个程度"。
42
+ * 只在自己拼路径、不从用户输入取路径时声明它。
43
+ */
44
+ export function openPath(path, openWith) {
45
+ return invoke('opener.openPath', openWith === undefined ? { path } : { path, with: openWith });
46
+ }
47
+
48
+ /**
49
+ * 在系统文件管理器里选中若干文件(Windows 资源管理器 / macOS 访达 / Linux 文件管理器)。
50
+ *
51
+ * @param {string | string[]} path 一个路径或一组路径。
52
+ * @returns {Promise<boolean>} 是否**全部**定位成功。
53
+ * @remarks
54
+ * 返回布尔而不是 void(Tauri 是 void):一个失败时调用方会以为"都定位到了"。
55
+ * 全部成功才算成功。
56
+ */
57
+ export function revealItemInDir(path) {
58
+ // **先校验再包装**:反过来的话空字符串会被包成 `['']`(长度 1),
59
+ // 于是一条本该在页面侧就被挡下的空路径会一路发到宿主——
60
+ // 而宿主的报错是"缺失参数"或"路径不存在",指不到"你传了个空串"。
61
+ if (typeof path === 'string') {
62
+ if (path === '') {
63
+ throw new TypeError('revealItemInDir: path 不能为空字符串');
64
+ }
65
+
66
+ return invoke('opener.revealItemInDir', { paths: [path] });
67
+ }
68
+
69
+ if (!Array.isArray(path) || path.length === 0) {
70
+ throw new TypeError('revealItemInDir: path 必须是非空字符串或非空数组');
71
+ }
72
+
73
+ return invoke('opener.revealItemInDir', { paths: path });
74
+ }
package/src/path.js ADDED
@@ -0,0 +1,335 @@
1
+ // 对标 `@tauri-apps/api/path` 里的**路径运算**部分。
2
+ //
3
+ // 一个反直觉的事实(读官方源码才确认):Tauri 2 里除 `sep()` / `delimiter()` 外,
4
+ // **全部**路径运算都走 IPC,交给宿主的 `std::path`。所以本模块也是这样——
5
+ // 只有那两个常量是同步的。
6
+ //
7
+ // 这样做的理由不是照抄,而是平台路径规则的正确性:Windows 的盘符、UNC、`\\?\` 前缀、
8
+ // 大小写不敏感,POSIX 的重复斜杠与尾随斜杠…… 在 JS 里手写一份就是又造一份平台语义,
9
+ // 而两份实现必然在某个边角上不一致,症状是"只在某个平台错"。宿主有 .NET 的 Path,
10
+ // 那是这些规则唯一的权威实现。
11
+ //
12
+ // 本模块**不做文件系统 I/O**:`normalize` / `resolve` 是词法归一(见下),不解析符号链接。
13
+
14
+ import { invoke } from './core.js';
15
+ import { getTransport } from './internal.js';
16
+
17
+ /**
18
+ * 平台路径分隔符:Windows 上是 `\`,POSIX 上是 `/`。
19
+ *
20
+ * @returns {string}
21
+ * @remarks **同步**(读宿主注入的常量),所以不必 await。Tauri 亦然。
22
+ */
23
+ export function sep() {
24
+ return /** @type {any} */ (getTransport()).path?.sep ?? '/';
25
+ }
26
+
27
+ /**
28
+ * 平台路径分隔**符串**(PATH 环境变量里那种):Windows 上是 `;`,POSIX 上是 `:`。
29
+ *
30
+ * @returns {string}
31
+ * @remarks
32
+ * 名字容易与 `sep()` 混:`sep` 是**一个路径内**的分隔符,`delimiter` 是**多个路径之间**的分隔符。
33
+ * 同步,理由同 `sep()`。
34
+ */
35
+ export function delimiter() {
36
+ return /** @type {any} */ (getTransport()).path?.delimiter ?? ':';
37
+ }
38
+
39
+ /**
40
+ * 把若干段拼成一个路径,用平台分隔符连接。
41
+ *
42
+ * @param {...string} paths
43
+ * @returns {Promise<string>}
44
+ */
45
+ export function join(...paths) {
46
+ return invoke('path.join', { paths });
47
+ }
48
+
49
+ /**
50
+ * 归一化路径:展开 `.` / `..`、统一分隔符、拼成绝对路径。
51
+ *
52
+ * @param {string} path
53
+ * @returns {Promise<string>}
54
+ * @remarks
55
+ * **与 Tauri 的差别(刻意,且是改进)**:Tauri 的 `normalize` 用 `std::fs::canonicalize`,
56
+ * 它会解析符号链接,而且**路径不存在时直接报错**——"把用户输入的路径先归一一下"
57
+ * 在路径还不存在时就会失败,是个很容易踩的坑。
58
+ * 本库用词法归一,**不做任何文件系统访问**,所以对不存在的路径也能正确工作。
59
+ * 需要"真实路径"(解析链接)应当是一个显式的 fs 操作,而不是 `normalize` 偷偷做的事。
60
+ */
61
+ export function normalize(path) {
62
+ return invoke('path.normalize', { path });
63
+ }
64
+
65
+ /**
66
+ * 相对 `path` 的父目录;结尾的分隔符被忽略。
67
+ *
68
+ * @param {string} path
69
+ * @returns {Promise<string>}
70
+ * @remarks
71
+ * 已经是根(`/` 或 `C:\`)时返回**空串**(Tauri 的 `Path::parent()` 给 `None`,
72
+ * 序列化成空串)。调用方据此判断"到头了"。
73
+ */
74
+ export function dirname(path) {
75
+ return invoke('path.dirname', { path });
76
+ }
77
+
78
+ /**
79
+ * 扩展名,**不带点**(`'html'`,不是 `'.html'`)。
80
+ *
81
+ * @param {string} path
82
+ * @returns {Promise<string>}
83
+ * @remarks
84
+ * 两处与 Node 的 `path.extname` 不同,都是跟 Tauri/Rust 而不是跟 Node:
85
+ * 不含前导点;没有扩展名时返回**空串**而不是抛错。
86
+ * 以点开头的隐藏文件(`.gitignore`)扩展名是空串——整名算文件名。
87
+ */
88
+ export function extname(path) {
89
+ return invoke('path.extname', { path });
90
+ }
91
+
92
+ /**
93
+ * 末段文件名。
94
+ *
95
+ * @param {string} path
96
+ * @param {string} [ext] 要从结果里去掉的扩展名(**区分大小写**,通常别带前导点)。
97
+ * @returns {Promise<string>}
98
+ */
99
+ export function basename(path, ext) {
100
+ return invoke('path.basename', ext === undefined ? { path } : { path, ext });
101
+ }
102
+
103
+ /**
104
+ * 是否绝对路径。
105
+ *
106
+ * @param {string} path
107
+ * @returns {Promise<boolean>}
108
+ * @remarks
109
+ * 判据是**平台相关**的:Windows 上 `/foo` 算绝对(有根无盘符),
110
+ * 而 `foo\bar` 不算。所以这里也交给宿主——JS 里判这个必然在某个平台上判错。
111
+ */
112
+ export function isAbsolute(path) {
113
+ return invoke('path.isAbsolute', { path });
114
+ }
115
+
116
+ /**
117
+ * 把若干段解析成一个绝对路径。
118
+ *
119
+ * @param {...string} paths
120
+ * @returns {Promise<string>}
121
+ * @remarks
122
+ * 与 `join()` 的差别:本函数**保证结果是绝对路径**(相对路径会拼上当前工作目录),
123
+ * `join()` 则可能给出相对路径。同理不做符号链接解析(见 `normalize`)。
124
+ */
125
+ export function resolve(...paths) {
126
+ return invoke('path.resolve', { paths });
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // 目录解析(对标 Tauri 的 appDataDir / homeDir / … 那一整组)
131
+ //
132
+ // **命令名与 Tauri 的函数名一一对应**(path.appDataDir ↔ appDataDir()),
133
+ // 所以下面这些函数体全是同一形状,不必逐个写实现。
134
+ //
135
+ // 三平台的解析规则各格不同(Linux 走 XDG / macOS 走 ~/Library / Windows 走 FOLDERID_*),
136
+ // 由**宿主**算——理由与上面那些路径运算一样,而且这里更强:XDG 的用户目录
137
+ // 是用户可配置的(中文系统上"桌面"常常就是 ~/桌面),Windows 的已知文件夹
138
+ // 也可以被重定向到别的盘。在 JS 里拼字符串必然在某个用户的机器上错。
139
+ // ---------------------------------------------------------------------------
140
+
141
+ /**
142
+ * 生成一个目录函数:命令名 = `path.` + name + `Dir`。
143
+ *
144
+ * @param {string} name 目录名(与 Tauri 的 BaseDirectory 取值同名,首字母大写)。
145
+ * @returns {() => Promise<string>}
146
+ */
147
+ function dirFunction(name) {
148
+ // 目录名的首字母在 JS 里取:`name[0]` 在 noUncheckedIndexedAccess 下是 string|undefined,
149
+ // 而这些名字是模块内的字面量,不会空。用 charAt 表达"一定有一个字符"更直白,
150
+ // 也免了一个只为了哄类型检查器的非空断言。
151
+ const command = `path.${name.charAt(0).toLowerCase()}${name.slice(1)}Dir`;
152
+ return () => invoke(command);
153
+ }
154
+
155
+ /**
156
+ * 应用配置文件目录:`${configDir}/${identifier}`。对标 Tauri 的 `appConfigDir()`。
157
+ *
158
+ * @type {() => Promise<string>}
159
+ */
160
+ export const appConfigDir = dirFunction('AppConfig');
161
+
162
+ /** 应用数据目录:`${dataDir}/${identifier}`。 */
163
+ export const appDataDir = dirFunction('AppData');
164
+
165
+ /** 应用本地数据目录:`${localDataDir}/${identifier}`。 */
166
+ export const appLocalDataDir = dirFunction('AppLocalData');
167
+
168
+ /** 应用缓存目录:`${cacheDir}/${identifier}`。 */
169
+ export const appCacheDir = dirFunction('AppCache');
170
+
171
+ /**
172
+ * 应用日志目录。
173
+ *
174
+ * @remarks
175
+ * 三平台规则里唯一一个全都不同的:macOS 是 `~/Library/Logs/{identifier}`(**不在**
176
+ * 应用数据目录下),另两个平台是 `${localDataDir}/${identifier}/logs`。
177
+ */
178
+ export const appLogDir = dirFunction('AppLog');
179
+
180
+ /** 用户的音乐目录。Linux `XDG_MUSIC_DIR` / macOS `~/Music` / Windows `{FOLDERID_Music}`。 */
181
+ export const audioDir = dirFunction('Audio');
182
+
183
+ /** 用户的缓存目录。Linux `$XDG_CACHE_HOME` 或 `~/.cache` / macOS `~/Library/Caches` / Windows `{FOLDERID_LocalAppData}`。 */
184
+ export const cacheDir = dirFunction('Cache');
185
+
186
+ /** 用户的配置目录。Linux `$XDG_CONFIG_HOME` 或 `~/.config` / macOS `~/Library/Application Support` / Windows `{FOLDERID_RoamingAppData}`。 */
187
+ export const configDir = dirFunction('Config');
188
+
189
+ /** 用户的数据目录。Linux `$XDG_DATA_HOME` 或 `~/.local/share` / macOS `~/Library/Application Support` / Windows `{FOLDERID_RoamingAppData}`。 */
190
+ export const dataDir = dirFunction('Data');
191
+
192
+ /** 用户的本地数据目录。Windows 上是 `{FOLDERID_LocalAppData}`(与 `dataDir` **不同**)。 */
193
+ export const localDataDir = dirFunction('LocalData');
194
+
195
+ /** 桌面目录。 */
196
+ export const desktopDir = dirFunction('Desktop');
197
+
198
+ /** 文档目录。 */
199
+ export const documentDir = dirFunction('Document');
200
+
201
+ /** 下载目录。 */
202
+ export const downloadDir = dirFunction('Download');
203
+
204
+ /**
205
+ * 可执行文件目录。
206
+ *
207
+ * @remarks
208
+ * **只有 Linux 支持**(`$XDG_BIN_HOME` 或 `~/.local/bin`);macOS 与 Windows 上调用会
209
+ * 以 `not_supported` 被拒——这一点与 Tauri 一致。请按平台分支。
210
+ */
211
+ export const executableDir = dirFunction('Executable');
212
+
213
+ /**
214
+ * 字体目录。
215
+ *
216
+ * @remarks
217
+ * Linux `$XDG_DATA_HOME/fonts`、macOS `~/Library/Fonts`;**Windows 上不支持**(与 Tauri 一致)。
218
+ */
219
+ export const fontDir = dirFunction('Font');
220
+
221
+ /** 家目录。 */
222
+ export const homeDir = dirFunction('Home');
223
+
224
+ /** 图片目录。 */
225
+ export const pictureDir = dirFunction('Picture');
226
+
227
+ /** 公共目录。Windows 上是 `{FOLDERID_Public}`(`C:\Users\Public`,**不是** `...\Documents`)。 */
228
+ export const publicDir = dirFunction('Public');
229
+
230
+ /**
231
+ * 资源目录(随应用分发的额外文件,如数据库模板、脚本)。
232
+ *
233
+ * @remarks
234
+ * Windows 是主可执行文件所在目录;macOS 是 `${exe_dir}/../Resources`;
235
+ * Linux 是 `$APPDIR/usr/lib/{exe}`(AppImage)或 `/usr/lib/{exe}`。
236
+ *
237
+ * 要拼具体文件用 {@link resolveResource}——它有越界校验。
238
+ */
239
+ export const resourceDir = dirFunction('Resource');
240
+
241
+ /**
242
+ * 运行时目录。
243
+ *
244
+ * @remarks
245
+ * **只有 Linux 支持**(`$XDG_RUNTIME_DIR`);macOS 与 Windows 上以 `not_supported` 被拒。
246
+ * Linux 上若该环境变量缺失也会被拒——**不会**退到 `/tmp`:
247
+ * `/tmp` 是全局共享的,不是"本用户的运行时目录"那个语义,给个错值比明确失败糟得多。
248
+ */
249
+ export const runtimeDir = dirFunction('Runtime');
250
+
251
+ /** 临时目录。 */
252
+ export const tempDir = dirFunction('Temp');
253
+
254
+ /**
255
+ * 模板目录。
256
+ *
257
+ * @remarks Linux `XDG_TEMPLATES_DIR`、Windows `{FOLDERID_Templates}`;**macOS 上不支持**(与 Tauri 一致)。
258
+ */
259
+ export const templateDir = dirFunction('Template');
260
+
261
+ /** 视频目录。 */
262
+ export const videoDir = dirFunction('Video');
263
+
264
+ /**
265
+ * 把一段相对路径解析到资源目录下(对标 Tauri 的 `resolveResource()`)。
266
+ *
267
+ * @param {string} resourcePath 相对资源目录的路径;子目录与 `..` 都允许,但不能跑出资源目录。
268
+ * @returns {Promise<string>}
269
+ * @remarks
270
+ * 与 `join(await resourceDir(), p)` 的差别是**这里有校验**:绝对路径被拒
271
+ * (那等于"读文件系统任意位置",而函数名是 resolve**Resource**),
272
+ * 拼完跑出资源目录的也拒。判据是"拼完在不在根之内"而不是"字符串里有没有 `..`"——
273
+ * `assets/../logo.svg` 是合法的、也很常见。
274
+ */
275
+ export function resolveResource(resourcePath) {
276
+ return invoke('path.resolveResource', { path: resourcePath });
277
+ }
278
+
279
+ /**
280
+ * 目录枚举(对标 Tauri 的 `BaseDirectory`)——给 `fs` 系列的 `baseDir` 选项用:
281
+ * 相对路径按枚举解析到对应目录(绝对路径不参与)。
282
+ *
283
+ * 取值与 Tauri 逐键同值(1–22),所以迁移代码里的 `BaseDirectory.AppData` 不必改。
284
+ *
285
+ * @readonly
286
+ */
287
+ export const BaseDirectory = Object.freeze({
288
+ Audio: 1,
289
+ Cache: 2,
290
+ Config: 3,
291
+ Data: 4,
292
+ LocalData: 5,
293
+ Document: 6,
294
+ Download: 7,
295
+ Picture: 8,
296
+ Public: 9,
297
+ Video: 10,
298
+ Resource: 11,
299
+ Temp: 12,
300
+ AppConfig: 13,
301
+ AppData: 14,
302
+ AppLocalData: 15,
303
+ AppCache: 16,
304
+ AppLog: 17,
305
+ Desktop: 18,
306
+ Executable: 19,
307
+ Font: 20,
308
+ Home: 21,
309
+ Runtime: 22,
310
+ Template: 23,
311
+ // 反向映射(与 UserAttentionType 同一理由):迁移代码里两种写法都可能出现
312
+ 1: 'Audio',
313
+ 2: 'Cache',
314
+ 3: 'Config',
315
+ 4: 'Data',
316
+ 5: 'LocalData',
317
+ 6: 'Document',
318
+ 7: 'Download',
319
+ 8: 'Picture',
320
+ 9: 'Public',
321
+ 10: 'Video',
322
+ 11: 'Resource',
323
+ 12: 'Temp',
324
+ 13: 'AppConfig',
325
+ 14: 'AppData',
326
+ 15: 'AppLocalData',
327
+ 16: 'AppCache',
328
+ 17: 'AppLog',
329
+ 18: 'Desktop',
330
+ 19: 'Executable',
331
+ 20: 'Font',
332
+ 21: 'Home',
333
+ 22: 'Runtime',
334
+ 23: 'Template',
335
+ });