@orielweb/api 0.0.0-stage → 0.4.15

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.
@@ -0,0 +1,22 @@
1
+ // 全局形状:注入脚本装到页面上的 `window.oriel`。
2
+ //
3
+ // 单独声明而不是在 `core.js` 里到处 `/** @type {any} */` 断言:
4
+ // 断言一多,"这个字段到底存不存在"就没人知道了。声明之后,`window.oriel.label`
5
+ // 这类访问在**编译期**就会被检查——而 `label` 正是 P0-2 要加的东西,
6
+ // 早声明能让"还没实现"变成一个可见的类型问题,而不是运行期的 undefined。
7
+
8
+ import type { OrielTransport } from './types.js';
9
+
10
+ declare global {
11
+ interface Window {
12
+ /**
13
+ * OrielWeb 的注入式传输层。
14
+ *
15
+ * 只在**可信来源**的文档里存在(见 docs/API.md §4):远程页面里连这个对象都没有。
16
+ * 页面代码请 import `@orielweb/api/*`,不要直接用这个对象。
17
+ */
18
+ oriel?: OrielTransport;
19
+ }
20
+ }
21
+
22
+ export {};
@@ -0,0 +1,147 @@
1
+ // 图像字节的共享工具:`window.js`(setIcon 的三种字节写法)与 `image.js`(Image 类)都用它。
2
+ //
3
+ // **不进包的 `exports`**:这不是公开 API,页面不该 import 它。放在单独文件里是因为
4
+ // 两个模块都要用,而复制两份的话"越界字节要不要拦""分块大小取多少"这种判断
5
+ // 迟早会在两边分叉——那种分叉的症状是"同一个图标用 setIcon 传和用 Image 传效果不同"。
6
+ //
7
+ // 与 `internal.js` 分开:那个只管传输层(window.oriel),跟图像无关。
8
+
9
+ /**
10
+ * 把 Tauri `JsImage` 里的三种"字节"写法归一成 `Uint8Array`;不是字节形式就返回 `null`。
11
+ *
12
+ * @param {any} value
13
+ * @returns {Uint8Array | null}
14
+ * @remarks
15
+ * `number[]` 的每一项都要校验成 0..255 的整数:`new Uint8Array([999])` 会**静默截断**
16
+ * 成 231,于是页面拿到一个"没报错但图标不对"的结果。这类输入通常来自
17
+ * `Array.from(fileReader.result)` 或手写的像素数组,写错一位就会被截断。
18
+ */
19
+ export function toImageBytes(value) {
20
+ if (value instanceof Uint8Array) {
21
+ return value;
22
+ }
23
+
24
+ if (value instanceof ArrayBuffer) {
25
+ return new Uint8Array(value);
26
+ }
27
+
28
+ // TypedArray / DataView 一族(Tauri 的类型只写了 Uint8Array,但别的视图在运行时同样常见)
29
+ if (ArrayBuffer.isView(value)) {
30
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
31
+ }
32
+
33
+ if (Array.isArray(value)) {
34
+ for (let i = 0; i < value.length; i++) {
35
+ const byte = value[i];
36
+ if (typeof byte !== 'number' || !Number.isInteger(byte) || byte < 0 || byte > 255) {
37
+ throw new TypeError(
38
+ `字节数组的第 ${i} 项不是 0..255 的整数(收到 ${String(byte)})——` +
39
+ '它会被 Uint8Array 静默截断,所以这里直接拒。');
40
+ }
41
+ }
42
+
43
+ return Uint8Array.from(value);
44
+ }
45
+
46
+ return null;
47
+ }
48
+
49
+ /**
50
+ * `Uint8Array` → base64。
51
+ *
52
+ * @param {Uint8Array} bytes
53
+ * @returns {string}
54
+ * @remarks
55
+ * 不用 `btoa(String.fromCharCode(...bytes))`:那样每个字节都变成一个实参,
56
+ * 图标这种几十 KB 的输入会**爆栈**。分块是这里唯一需要小心的地方
57
+ * (0x4000 留了余量,V8 的实参上限约 65535)。
58
+ */
59
+ export function bytesToBase64(bytes) {
60
+ const CHUNK = 0x4000;
61
+ let binary = '';
62
+
63
+ for (let i = 0; i < bytes.length; i += CHUNK) {
64
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
65
+ }
66
+
67
+ return btoa(binary);
68
+ }
69
+
70
+ /**
71
+ * base64 → `Uint8Array`。
72
+ *
73
+ * @param {string} base64
74
+ * @returns {Uint8Array}
75
+ * @throws {TypeError} 不是合法的 base64。
76
+ */
77
+ export function base64ToBytes(base64) {
78
+ let binary;
79
+ try {
80
+ binary = atob(base64);
81
+ } catch {
82
+ throw new TypeError('图像数据不是合法的 base64');
83
+ }
84
+
85
+ const bytes = new Uint8Array(binary.length);
86
+ for (let i = 0; i < binary.length; i++) {
87
+ bytes[i] = binary.charCodeAt(i);
88
+ }
89
+
90
+ return bytes;
91
+ }
92
+
93
+ /** PNG 的 8 字节签名。 */
94
+ const PNG_SIGNATURE = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
95
+
96
+ /**
97
+ * 猜图像格式:只认 PNG 与 ICO(与 Tauri 的 `image-png` / `image-ico` 两个 feature 对应)。
98
+ *
99
+ * @param {Uint8Array} bytes
100
+ * @returns {'png' | 'ico' | null} 认不出时返回 `null`。
101
+ * @remarks
102
+ * 只读文件头,不做完整解码——解码在宿主侧(`OrielImage.FromBytes`),
103
+ * 页面这边提前判一次是为了让 `Image.fromBytes(乱数据)` 能**当场**给出可排查的错误,
104
+ * 而不是等一次 IPC 往返之后再拿到一个语焉不详的失败。
105
+ */
106
+ export function sniffImageFormat(bytes) {
107
+ if (bytes.length >= 8 && PNG_SIGNATURE.every((byte, index) => bytes[index] === byte)) {
108
+ return 'png';
109
+ }
110
+
111
+ // ICO/CUR:ICONDIR 的前两个字节是保留位 0,接着是类型(1 = icon,2 = cursor)。
112
+ // 这比"看扩展名"可靠——字节形式本来就没有扩展名。
113
+ if (bytes.length >= 6 && bytes[0] === 0 && bytes[1] === 0 && (bytes[2] === 1 || bytes[2] === 2)) {
114
+ return 'ico';
115
+ }
116
+
117
+ return null;
118
+ }
119
+
120
+ /**
121
+ * 校验一段字节是不是可识别的图像;不是就抛。
122
+ *
123
+ * @param {Uint8Array} bytes
124
+ * @param {string} context 出错信息里的前缀(例如 `setIcon`),便于定位调用方。
125
+ * @returns {void}
126
+ */
127
+ export function assertImageFormat(bytes, context) {
128
+ if (bytes.length === 0) {
129
+ throw new TypeError(`${context}: 图像数据是空的`);
130
+ }
131
+
132
+ if (sniffImageFormat(bytes) === null) {
133
+ throw new TypeError(
134
+ `${context}: 认不出图像格式——本库只支持 PNG 与 ICO` +
135
+ `(收到 ${bytes.length} 字节,开头是 ${hexPreview(bytes)})。`);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * 取开头几个字节的十六进制,用于错误信息。
141
+ *
142
+ * @param {Uint8Array} bytes
143
+ * @returns {string}
144
+ */
145
+ function hexPreview(bytes) {
146
+ return Array.from(bytes.subarray(0, 8), (byte) => byte.toString(16).padStart(2, '0')).join(' ');
147
+ }
package/src/image.js ADDED
@@ -0,0 +1,407 @@
1
+ // `@orielweb/api/image`:对标 `@tauri-apps/api/image`。
2
+ //
3
+ // **本库的 `Image` 与 Tauri 的有一处本质区别,写在最前面**:
4
+ //
5
+ // Tauri 的 `Image` 是**资源表 handle**(继承 `Resource`,持有 Rust 侧的一个 `rid`),
6
+ // 字节留在 Rust 那边;它还有一个 `close()` 用来释放那条记录。
7
+ //
8
+ // 本库**没有资源表**——IPC 是一次性请求-回执,没有"把对象存到宿主那边再引用它"的
9
+ // 协议。所以这里的 `Image` 是一个**纯 JS 的字节容器**:它在页面侧持有
10
+ // `Uint8Array`,传给宿主时仍然是完整字节(base64 走 `window.setIcon` 的 `bytes` 参数)。
11
+ //
12
+ // 这个差别带来三个可观察的后果,迁移时要心里有数:
13
+ // 1. **没有 `close()`**:没有宿主侧资源可释放,给一个空方法会让调用方误以为"不关就泄漏";
14
+ // 2. **`rgba()` / `size()` 是同步可用的本地值**(Tauri 那边是异步的——它要往 Rust 问)。
15
+ // 签名仍保持 `Promise`,所以 `await image.rgba()` 这种写法照样能跑;
16
+ // 3. **跨 IPC 传 `Image` 时体积等于字节本身**(Tauri 只传一个 `rid`)。图标这种
17
+ // 几十 KB 的量级无所谓;几百 KB 的大图走这条路会明显变慢。
18
+ //
19
+ // 另一处细节:**`fromBytes` / `fromPath` 需要能读出尺寸**。尺寸就在 PNG 的 IHDR 头里,
20
+ // 这里直接解析那几个字节(PNG 与 ICO 都解析),因此**不需要宿主参与**——
21
+ // 这是"纯 JS 容器"能成立的关键,也是它和 Tauri 行为上最接近的地方。
22
+
23
+ import { invoke } from './core.js';
24
+ import { warnCompat } from './internal.js';
25
+ import {
26
+ toImageBytes,
27
+ bytesToBase64,
28
+ base64ToBytes,
29
+ sniffImageFormat,
30
+ assertImageFormat,
31
+ } from './image-bytes.js';
32
+
33
+ /**
34
+ * @typedef {{ width: number, height: number }} ImageSize
35
+ */
36
+
37
+ /**
38
+ * Tauri 的 `JsImage`:可以传给需要图像的地方(`Window.setIcon` 等)的各种形式。
39
+ *
40
+ * @typedef {string | Uint8Array | ArrayBuffer | number[] | Image} JsImage
41
+ */
42
+
43
+ /**
44
+ * 一张 RGBA 图像(行优先、自上而下)。
45
+ *
46
+ * @remarks
47
+ * 与 Tauri 的 `Image` 不同,本库的这个类是**字节容器**而不是资源表 handle——
48
+ * 理由见本文件开头那段。对外形状(`new` / `fromBytes` / `fromPath` / `rgba` / `size`)
49
+ * 与 Tauri 保持一致,所以迁移代码不用改。
50
+ *
51
+ * @example
52
+ * ```js
53
+ * import { Image } from '@orielweb/api/image';
54
+ * import { getCurrentWindow } from '@orielweb/api/window';
55
+ *
56
+ * const icon = await Image.fromPath('icons/icon.png');
57
+ * await getCurrentWindow().setIcon(icon);
58
+ * ```
59
+ */
60
+ export class Image {
61
+ /**
62
+ * 由字节与尺寸构造。**不直接用**——用静态工厂(它们会解析尺寸并校验格式)。
63
+ *
64
+ * @param {Uint8Array} bytes 原始 PNG/ICO 字节。
65
+ * @param {number} width
66
+ * @param {number} height
67
+ */
68
+ constructor(bytes, width, height) {
69
+ if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
70
+ throw new TypeError('Image: bytes 必须是非空的 Uint8Array');
71
+ }
72
+
73
+ if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
74
+ throw new TypeError(`Image: 尺寸必须是正整数(收到 ${width}x${height})`);
75
+ }
76
+
77
+ /** 原始编码字节(PNG/ICO)。 */
78
+ this.bytes = bytes;
79
+
80
+ /**
81
+ * 标记:让 `window.setIcon` 能**不依赖 `instanceof`** 认出这是 Image(见那处的说明)。
82
+ * @type {true}
83
+ */
84
+ this.isOrielImage = true;
85
+
86
+ /**
87
+ * 是否持有**编码字节**(PNG/ICO)。
88
+ * @remarks
89
+ * `Image.new` 用裸 RGBA 建图,那种图没有编码形式能给宿主,所以 `setIcon` 会拒绝它。
90
+ * 这个标记用来区分两种来源(默认 false,静态工厂里改)。
91
+ * @type {boolean}
92
+ */
93
+ this._hasEncodedBytes = false;
94
+
95
+ /** @type {ImageSize} */
96
+ this._size = { width, height };
97
+
98
+ // 解码过的 RGBA 缓存。`rgba()` 可能被反复调用(比如逐帧改像素前先取一份),
99
+ // 每次重启解码既慢又没必要。
100
+ /** @type {Uint8Array | null} */
101
+ this._rgba = null;
102
+ }
103
+
104
+ /**
105
+ * 用**原始 RGBA 数据**建一张图(不解码任何格式,因此也不要求是 PNG/ICO)。
106
+ *
107
+ * @param {number[] | Uint8Array | ArrayBuffer} rgba 行优先的 RGBA,长度必须正好 `width * height * 4`。
108
+ * @param {number} width
109
+ * @param {number} height
110
+ * @returns {Promise<Image>}
111
+ * @remarks
112
+ * 与 Tauri 一致返回 `Promise`。
113
+ *
114
+ * **这条返回的 Image 可以直接喂给 `setIcon`**:宿主侧的图标工厂本来就吃 RGBA
115
+ * (`OrielImage` 是 RGBA 容器,构造时不做格式解码),所以 `setIcon` 会走
116
+ * `rgba` + 尺寸那条参数,**不需要先把像素编码成 PNG**。
117
+ */
118
+ static async new(rgba, width, height) {
119
+ const bytes = toImageBytes(rgba);
120
+ if (bytes === null) {
121
+ throw new TypeError('Image.new: rgba 必须是 number[]、Uint8Array 或 ArrayBuffer');
122
+ }
123
+
124
+ if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
125
+ throw new TypeError(`Image.new: 尺寸必须是正整数(收到 ${width}x${height})`);
126
+ }
127
+
128
+ const expected = width * height * 4;
129
+ if (bytes.length !== expected) {
130
+ throw new TypeError(
131
+ `Image.new: 缓冲区应当是 ${width}x${height}x4 = ${expected} 字节,收到 ${bytes.length} 字节`);
132
+ }
133
+
134
+ const image = new Image(new Uint8Array(bytes), width, height);
135
+ image._rgba = bytes;
136
+ // 从 RGBA 建出来的图没有编码字节,_encoded 为 false 时 setIcon 会明确拒绝。
137
+ image._hasEncodedBytes = false;
138
+ return image;
139
+ }
140
+
141
+ /**
142
+ * 从 PNG/ICO 字节建图(自动识别格式并解析尺寸)。
143
+ *
144
+ * @param {number[] | Uint8Array | ArrayBuffer} bytes
145
+ * @returns {Promise<Image>}
146
+ * @throws {TypeError} 不是 PNG/ICO,或头部损坏。
147
+ */
148
+ static async fromBytes(bytes) {
149
+ const raw = toImageBytes(bytes);
150
+ if (raw === null) {
151
+ throw new TypeError('Image.fromBytes: bytes 必须是 number[]、Uint8Array 或 ArrayBuffer');
152
+ }
153
+
154
+ assertImageFormat(raw, 'Image.fromBytes');
155
+ const { width, height } = readSize(raw);
156
+
157
+ const image = new Image(new Uint8Array(raw), width, height);
158
+ image._hasEncodedBytes = true;
159
+ return image;
160
+ }
161
+
162
+ /**
163
+ * 从**文件路径**建图(路径由宿主解析,因此是用户机器上的真实路径)。
164
+ *
165
+ * @param {string} path
166
+ * @returns {Promise<Image>}
167
+ * @remarks
168
+ * 与 `setIcon(path)` 的区别:这条会把字节**读进页面**,之后可以反复用、
169
+ * 也可以用 `rgba()` 看像素;而 `setIcon(path)` 只把路径交给宿主,页面拿不到字节。
170
+ * 代价是多一次 IPC 往返与一份页面侧内存。
171
+ */
172
+ static async fromPath(path) {
173
+ if (typeof path !== 'string' || path === '') {
174
+ throw new TypeError('Image.fromPath: path 必须是非空字符串');
175
+ }
176
+
177
+ // 宿主返回 base64(见 window.readImageFile 命令)。
178
+ const base64 = await invoke('window.readImageFile', { path });
179
+ const bytes = base64ToBytes(base64);
180
+ assertImageFormat(bytes, 'Image.fromPath');
181
+
182
+ const { width, height } = readSize(bytes);
183
+ const image = new Image(bytes, width, height);
184
+ image._hasEncodedBytes = true;
185
+ return image;
186
+ }
187
+
188
+ /**
189
+ * 这张图的尺寸(像素)。
190
+ *
191
+ * @returns {Promise<ImageSize>}
192
+ * @remarks 与 Tauri 同签名(那边是异步的,因为它要问宿主)。本库直接给本地值。
193
+ */
194
+ async size() {
195
+ return { width: this._size.width, height: this._size.height };
196
+ }
197
+
198
+ /**
199
+ * 宽(像素)——同步读取,供 `setIcon` 这类内部路径用。
200
+ *
201
+ * @returns {number}
202
+ */
203
+ get width() {
204
+ return this._size.width;
205
+ }
206
+
207
+ /**
208
+ * 高(像素)——同步读取,供 `setIcon` 这类内部路径用。
209
+ *
210
+ * @returns {number}
211
+ */
212
+ get height() {
213
+ return this._size.height;
214
+ }
215
+
216
+ /**
217
+ * 是否持有**编码字节**(PNG/ICO)。
218
+ *
219
+ * @returns {boolean}
220
+ * @remarks
221
+ * `Image.new`(裸 RGBA)建的图是 `false`:那种图没有编码形式,
222
+ * 但**照样能设成图标**——宿主侧的图标工厂本来就吃 RGBA。
223
+ */
224
+ get hasEncodedBytes() {
225
+ return this._hasEncodedBytes;
226
+ }
227
+
228
+ /**
229
+ * 裸 RGBA 像素(`width * height * 4` 字节)。
230
+ *
231
+ * @returns {Uint8Array}
232
+ * @throws {TypeError} 这张图只有编码字节、还没解码过。
233
+ * @remarks
234
+ * 与 `rgba()` 的区别:这个是**同步**的,且**不会**触发解码。它给内部路径用
235
+ * (`setIcon` 要的是"现在就有没有像素"),而 `rgba()` 是公开 API、
236
+ * 会按需请宿主解码一次。
237
+ */
238
+ get rgbaBytes() {
239
+ if (this._rgba === null) {
240
+ throw new TypeError(
241
+ 'Image.rgbaBytes: 这张图还没有解码过的像素(编码字节的图请用 await rgba())');
242
+ }
243
+
244
+ return this._rgba;
245
+ }
246
+
247
+ /**
248
+ * 这张图的 RGBA 像素(行优先,`width * height * 4` 字节)。
249
+ *
250
+ * @returns {Promise<Uint8Array>}
251
+ * @remarks
252
+ * 与 Tauri 同签名。本库在**第一次调用时**请宿主解码一次(`window.decodeImage`),
253
+ * 之后缓存——因为 PNG 解码不是浏览器端该干的活,而宿主那边本来就有解码器
254
+ * (`OrielImage.FromBytes`)。
255
+ */
256
+ async rgba() {
257
+ if (this._rgba !== null) {
258
+ return this._rgba;
259
+ }
260
+
261
+ if (this._hasEncodedBytes === false) {
262
+ // Image.new 建的:_rgba 在构造时就填好了,走不到这里
263
+ throw new TypeError('Image.rgba: 这张图没有编码字节');
264
+ }
265
+
266
+ const base64 = await invoke('window.decodeImage', {
267
+ bytes: bytesToBase64(this.bytes),
268
+ });
269
+
270
+ this._rgba = base64ToBytes(base64);
271
+
272
+ const expected = this._size.width * this._size.height * 4;
273
+ if (this._rgba.length !== expected) {
274
+ // 宿主解出来的长度与页面解析的尺寸对不上——说明**两边的尺寸解析有分歧**。
275
+ // 宁可在这里报出来,也不要让调用方拿到一段错位的像素。
276
+ throw new Error(
277
+ `Image.rgba: 宿主解码得到 ${this._rgba.length} 字节,` +
278
+ `与 ${this._size.width}x${this._size.height}x4 = ${expected} 不符`);
279
+ }
280
+
281
+ return this._rgba;
282
+ }
283
+
284
+ /**
285
+ * 释放资源——**补壳**(B14):本库的 `Image` 只是页面侧的字节容器,
286
+ * 没有任何宿主侧资源可释放,调用不报错也不做任何事(迁移代码零删改)。
287
+ *
288
+ * debug 模式下打一条警告——**补壳必须可观测**,否则"close 了但什么都没发生"
289
+ * 会让人以为资源管理有了效果。
290
+ *
291
+ * @returns {Promise<void>}
292
+ */
293
+ close() {
294
+ warnCompat('Image.close 是补壳:本库的 Image 没有宿主资源可释放,该调用不生效。');
295
+ return Promise.resolve();
296
+ }
297
+ }
298
+
299
+ /**
300
+ * 读 PNG / ICO 的尺寸(不需要解码像素)。
301
+ *
302
+ * @param {Uint8Array} bytes
303
+ * @returns {ImageSize}
304
+ * @throws {TypeError} 头部不足以读出尺寸。
305
+ * @remarks
306
+ * PNG:IHDR 紧跟在 8 字节签名与 4 字节长度 + 4 字节类型之后,宽高各占 4 字节大端。
307
+ * ICO:ICONDIR(6 字节)后是第一个 ICONDIRENTRY,宽高在它的第 0、1 字节,
308
+ * **0 表示 256**(这是 ICO 格式的一个历史坑——字段只有 1 字节)。
309
+ */
310
+ function readSize(bytes) {
311
+ const format = sniffImageFormat(bytes);
312
+
313
+ if (format === 'png') {
314
+ if (bytes.length < 24) {
315
+ throw new TypeError('Image: PNG 头部不完整,读不出尺寸');
316
+ }
317
+
318
+ const width = readUInt32BigEndian(bytes, 16);
319
+ const height = readUInt32BigEndian(bytes, 20);
320
+ if (width === 0 || height === 0) {
321
+ throw new TypeError(`Image: PNG 头部给出的尺寸非法(${width}x${height})`);
322
+ }
323
+
324
+ return { width, height };
325
+ }
326
+
327
+ if (format === 'ico') {
328
+ if (bytes.length < 6 + 16) {
329
+ throw new TypeError('Image: ICO 头部不完整,读不出尺寸');
330
+ }
331
+
332
+ const count = (bytes[4] ?? 0) | ((bytes[5] ?? 0) << 8);
333
+ if (count === 0) {
334
+ throw new TypeError('Image: ICO 里没有任何图像条目');
335
+ }
336
+
337
+ // 0 在 ICO 里表示 256(字段只有 1 字节,装不下 256)
338
+ const widthByte = bytes[6] ?? 0;
339
+ const heightByte = bytes[7] ?? 0;
340
+ const width = widthByte === 0 ? 256 : widthByte;
341
+ const height = heightByte === 0 ? 256 : heightByte;
342
+ return { width, height };
343
+ }
344
+
345
+ throw new TypeError('Image: 认不出图像格式(只支持 PNG 与 ICO)');
346
+ }
347
+
348
+ /**
349
+ * 大端读 4 字节无符号整数。
350
+ *
351
+ * @param {Uint8Array} bytes
352
+ * @param {number} offset
353
+ * @returns {number}
354
+ */
355
+ function readUInt32BigEndian(bytes, offset) {
356
+ // `?? 0` 只是为了让类型收窄通过(`noUncheckedIndexedAccess` 把每次下标读都算成可能 undefined)。
357
+ // 调用方已经先查过长度,所以这里不会有真的越界。
358
+ const b0 = bytes[offset] ?? 0;
359
+ const b1 = bytes[offset + 1] ?? 0;
360
+ const b2 = bytes[offset + 2] ?? 0;
361
+ const b3 = bytes[offset + 3] ?? 0;
362
+ return ((b0 << 24) | (b1 << 16) | (b2 << 8) | b3) >>> 0;
363
+ }
364
+
365
+ /**
366
+ * 把 `Image` 转成可以放进 `invoke` 参数的形式(对标 Tauri 的 `transformImage`)。
367
+ *
368
+ * @param {JsImage | null} image
369
+ * @returns {any} 路径原样返回;字节编码成 base64;`Image` 取字节再编码;`null` 原样返回。
370
+ * @remarks
371
+ * Tauri 文档说这个函数的签名"不稳定",本库保持同样的形状只是为了**迁移时能编译**。
372
+ * 平时不需要它——`setIcon` 等入口自己会做转换。
373
+ */
374
+ export function transformImage(image) {
375
+ if (image === null) {
376
+ return null;
377
+ }
378
+
379
+ if (typeof image === 'string') {
380
+ return image;
381
+ }
382
+
383
+ if (isImageLike(image)) {
384
+ return bytesToBase64(/** @type {Image} */ (image).bytes);
385
+ }
386
+
387
+ const bytes = toImageBytes(image);
388
+ if (bytes !== null) {
389
+ return bytesToBase64(bytes);
390
+ }
391
+
392
+ throw new TypeError(
393
+ 'transformImage: 需要路径、PNG/ICO 字节,或 @orielweb/api/image 的 Image 实例');
394
+ }
395
+
396
+ /**
397
+ * 判断是不是本库的 `Image`(鸭子类型,理由见 `window.js` 里同名判断的说明)。
398
+ *
399
+ * @param {any} value
400
+ * @returns {boolean}
401
+ */
402
+ function isImageLike(value) {
403
+ return value !== null
404
+ && typeof value === 'object'
405
+ && value.isOrielImage === true
406
+ && value.bytes instanceof Uint8Array;
407
+ }
@@ -0,0 +1,81 @@
1
+ // 内部传输层:`window.oriel` 的唯一访问点。
2
+ //
3
+ // 定位与 Tauri 的 `__TAURI_INTERNALS__` 相同——**它不是公开 API**。公开 API 是
4
+ // `@orielweb/api/*` 各模块;`window.oriel` 只承担"把消息送进宿主"这一件事。
5
+ // 页面直接用它也能跑,但会绕过 JS 包提供的形状对齐(事件包装、DPI 换算、错误码),
6
+ // 因此文档不推荐。
7
+ //
8
+ // 单独成文件而不是各模块各自读 `window.oriel`,有两个具体原因:
9
+ // 1. `mocks` 模块要能把传输层整个换掉(Node 里没有 window.oriel),只有一处可换才换得干净;
10
+ // 2. 报错信息只有一份。"不在 OrielWeb 里运行"这个诊断必须说清怎么办,
11
+ // 而不是让调用方看到 `Cannot read properties of undefined`。
12
+
13
+ /** @typedef {import('./types.js').OrielTransport} OrielTransport */
14
+
15
+ /**
16
+ * 被 `mocks` 覆盖后的传输层;`null` 表示用真实的 `window.oriel`。
17
+ * @type {OrielTransport | null}
18
+ */
19
+ let transportOverride = null;
20
+
21
+ /**
22
+ * 取当前的传输层。
23
+ * @returns {OrielTransport}
24
+ * @throws {Error} 不在 OrielWeb 宿主里、且没有装 mock 时抛出。
25
+ */
26
+ export function getTransport() {
27
+ if (transportOverride !== null) {
28
+ return transportOverride;
29
+ }
30
+
31
+ const candidate = globalThis.window?.oriel;
32
+ if (!candidate || typeof candidate.invoke !== 'function') {
33
+ throw new Error(
34
+ 'window.oriel 不存在:这段代码不在 OrielWeb 宿主里运行,' +
35
+ '或当前文档来源不可信(桥接脚本只安装到可信来源,见 docs/API.md §4)。' +
36
+ '在 Node 里做单测请先调用 @orielweb/api/mocks 的 mockIPC。');
37
+ }
38
+
39
+ return candidate;
40
+ }
41
+
42
+ /**
43
+ * 覆盖传输层(`mocks` 用)。
44
+ * @param {OrielTransport | null} transport `null` 表示恢复真实传输层。
45
+ * @returns {void}
46
+ */
47
+ export function setTransportOverride(transport) {
48
+ transportOverride = transport;
49
+ }
50
+
51
+ /**
52
+ * 当前是否已装 mock。
53
+ * @returns {boolean}
54
+ */
55
+ export function hasTransportOverride() {
56
+ return transportOverride !== null;
57
+ }
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // 补壳警告(B14):`rid` / `setTempDirPath` / `setAutoResize` 这类"为迁移零删改而存在、
61
+ // 但对本库没有任何作用"的成员,**必须可观测**——否则"调用不报错但什么都没发生"
62
+ // 是最难查的一类问题。默认静默(零删改),`UseDebug()` 或 __setDebugWarnings(true) 下打警告。
63
+ // ---------------------------------------------------------------------------
64
+
65
+ let debugWarningsEnabled =
66
+ typeof globalThis !== 'undefined'
67
+ && (/** @type {Record<string, unknown>} */ (/** @type {unknown} */ (globalThis))).__ORIEL_DEBUG__ === true;
68
+
69
+ /** 测试钩子:开关补壳警告(双下划线命名法与 mocks 一致,不是公开 API)。 */
70
+ /** @param {boolean} enabled */
71
+ export function __setDebugWarnings(enabled) {
72
+ debugWarningsEnabled = enabled === true;
73
+ }
74
+
75
+ /** 补壳/平台差异的可观测信号:debug 模式下 console.warn,其余静默(保证零删改)。 */
76
+ /** @param {string} message */
77
+ export function warnCompat(message) {
78
+ if (debugWarningsEnabled) {
79
+ console.warn(`[orielweb] ${message}`);
80
+ }
81
+ }