ota-native-bridge 1.6.0 → 1.8.0

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 CHANGED
@@ -27,7 +27,7 @@ if (Native.isNativePlatform()) {
27
27
 
28
28
  **要点:**
29
29
 
30
- - **禁止 `async` / `await`**,用 `.then()` 或 callback;也不必先 `ready()`
30
+ - 送进 WebView 的脚本不要保留 `async` / `await`(旧 WebView 解析不了该语法;构建器已转译则不受影响,见 [docs/USAGE.md](./docs/USAGE.md))。接口用 `.then()` 或 callback;也不必先 `ready()`
31
31
  - `Native.isNativePlatform()` 判断是否在壳 App 里
32
32
  - 壳内打开外链用 `Browser.open(url)`,不要用 `<a target="_blank">`
33
33
  - 自行请求 Client API 时**必须**用 `Crypto.encryptRequestBody` / `postEncrypted`,公钥来自 `App.getInfo().platform.paramPublicKey`
@@ -52,6 +52,7 @@ if (Native.isNativePlatform()) {
52
52
  | 读设备标识 | `Device.getIdentifiers()`(须 `collectDeviceIdsAfterConsent`) |
53
53
  | 读壳生成的设备序列号 | `Device.getInstallDeviceId()`(UUID v4,与签名证书无关,不需要同意开关) |
54
54
  | 读分辨率 / 方向 / 锁屏 | `Device.getScreenInfo()` / `getOrientation()` / `getLockStatus()` |
55
+ | 设方向 / 全屏 | `Device.setOrientation()` / `Device.setFullscreen()` |
55
56
  | 读机型 / 系统 / CPU / Canvas / WebGL | `Device.getEnvironmentInfo()` 一次返回(含系统版本、厂商、型号);也可分别调用 `getInfo()` / `getSystemInfo()` / `getCpuInfo()` / `getCanvasRenderInfo()` / `getWebGlInfo()` |
56
57
  | 用资源包里的图换桌面图标 | `Icon.setFromResource({ path: 'icons/app-icon.png' })`(Android 8+;iOS / Harmony 返回失败) |
57
58
  | 加密 Client API 请求 | `Crypto.postEncrypted(url, plain, pem)` |
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.onNativeEvent = onNativeEvent;
4
+ const NATIVE_EVENT = 'nativebridgeevent';
5
+ function onNativeEvent(name, handler) {
6
+ if (typeof window === 'undefined') {
7
+ return () => undefined;
8
+ }
9
+ const listener = (e) => {
10
+ const detail = e.detail;
11
+ if (!detail || detail.name !== name) {
12
+ return;
13
+ }
14
+ try {
15
+ handler(detail.payload);
16
+ }
17
+ catch (error) {
18
+ // 业务回调异常不要影响后续事件
19
+ console.error('[ota-native-bridge] event handler failed', error);
20
+ }
21
+ };
22
+ window.addEventListener(NATIVE_EVENT, listener);
23
+ return () => window.removeEventListener(NATIVE_EVENT, listener);
24
+ }
package/dist/cjs/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Crypto = exports.postEncrypted = exports.encryptRequestBody = exports.isNativeBridgeAvailable = exports.getNativePostMessage = exports.decodeBridgePayload = exports.invokeNative = exports.isNativePlatform = exports.getPlatform = exports.whenReady = exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.Browser = exports.Ota = exports.App = exports.Native = exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
17
+ exports.Crypto = exports.postEncrypted = exports.encryptRequestBody = exports.isNativeBridgeAvailable = exports.getNativePostMessage = exports.decodeBridgePayload = exports.invokeNative = exports.isNativePlatform = exports.getPlatform = exports.whenReady = exports.onNativeEvent = exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.Browser = exports.Migration = exports.Ota = exports.App = exports.Native = exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
18
18
  __exportStar(require("./types"), exports);
19
19
  var version_1 = require("./version");
20
20
  Object.defineProperty(exports, "PACKAGE_NAME", { enumerable: true, get: function () { return version_1.PACKAGE_NAME; } });
@@ -23,6 +23,7 @@ var native_1 = require("./native");
23
23
  Object.defineProperty(exports, "Native", { enumerable: true, get: function () { return native_1.Native; } });
24
24
  Object.defineProperty(exports, "App", { enumerable: true, get: function () { return native_1.App; } });
25
25
  Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return native_1.Ota; } });
26
+ Object.defineProperty(exports, "Migration", { enumerable: true, get: function () { return native_1.Migration; } });
26
27
  Object.defineProperty(exports, "Browser", { enumerable: true, get: function () { return native_1.Browser; } });
27
28
  Object.defineProperty(exports, "Device", { enumerable: true, get: function () { return native_1.Device; } });
28
29
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return native_1.Config; } });
@@ -52,6 +53,8 @@ Object.defineProperty(exports, "Capture", { enumerable: true, get: function () {
52
53
  Object.defineProperty(exports, "Payment", { enumerable: true, get: function () { return native_1.Payment; } });
53
54
  Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return native_1.Store; } });
54
55
  Object.defineProperty(exports, "Push", { enumerable: true, get: function () { return native_1.Push; } });
56
+ var events_1 = require("./events");
57
+ Object.defineProperty(exports, "onNativeEvent", { enumerable: true, get: function () { return events_1.onNativeEvent; } });
55
58
  var runtime_1 = require("./runtime");
56
59
  Object.defineProperty(exports, "whenReady", { enumerable: true, get: function () { return runtime_1.whenReady; } });
57
60
  Object.defineProperty(exports, "getPlatform", { enumerable: true, get: function () { return runtime_1.getPlatform; } });
@@ -16,6 +16,9 @@ function getNativePostMessage() {
16
16
  if (window.HarmonyBridge?.postMessage) {
17
17
  return (message) => window.HarmonyBridge.postMessage(message);
18
18
  }
19
+ if (window.DesktopBridge?.postMessage) {
20
+ return (message) => window.DesktopBridge.postMessage(message);
21
+ }
19
22
  return null;
20
23
  }
21
24
  function isNativeBridgeAvailable() {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Crypto = exports.Volume = exports.Viewport = exports.Theme = exports.Store = exports.Share = exports.Sensor = exports.Scan = exports.Recorder = exports.Push = exports.Picker = exports.Payment = exports.Ota = exports.Notification = exports.Nfc = exports.Media = exports.Location = exports.Icon = exports.Http = exports.Haptics = exports.Flashlight = exports.Device = exports.Contacts = exports.Config = exports.Clipboard = exports.Capture = exports.Calendar = exports.Browser = exports.Brightness = exports.Bluetooth = exports.Biometric = exports.App = exports.Native = void 0;
3
+ exports.Crypto = exports.Volume = exports.Viewport = exports.Theme = exports.Store = exports.Share = exports.Sensor = exports.Scan = exports.Recorder = exports.Push = exports.Picker = exports.Payment = exports.Ota = exports.Notification = exports.Nfc = exports.Migration = exports.Media = exports.Location = exports.Icon = exports.Http = exports.Haptics = exports.Flashlight = exports.Device = exports.Contacts = exports.Config = exports.Clipboard = exports.Capture = exports.Calendar = exports.Browser = exports.Brightness = exports.Bluetooth = exports.Biometric = exports.App = exports.Native = void 0;
4
4
  const crypto_ns_1 = require("./crypto-ns");
5
5
  Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return crypto_ns_1.Crypto; } });
6
6
  const plugins_1 = require("./plugins");
@@ -21,6 +21,7 @@ Object.defineProperty(exports, "Http", { enumerable: true, get: function () { re
21
21
  Object.defineProperty(exports, "Icon", { enumerable: true, get: function () { return plugins_1.Icon; } });
22
22
  Object.defineProperty(exports, "Location", { enumerable: true, get: function () { return plugins_1.Location; } });
23
23
  Object.defineProperty(exports, "Media", { enumerable: true, get: function () { return plugins_1.Media; } });
24
+ Object.defineProperty(exports, "Migration", { enumerable: true, get: function () { return plugins_1.Migration; } });
24
25
  Object.defineProperty(exports, "Nfc", { enumerable: true, get: function () { return plugins_1.Nfc; } });
25
26
  Object.defineProperty(exports, "Notification", { enumerable: true, get: function () { return plugins_1.Notification; } });
26
27
  Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return plugins_1.Ota; } });
@@ -35,11 +36,13 @@ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { r
35
36
  Object.defineProperty(exports, "Theme", { enumerable: true, get: function () { return plugins_1.Theme; } });
36
37
  Object.defineProperty(exports, "Viewport", { enumerable: true, get: function () { return plugins_1.Viewport; } });
37
38
  Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return plugins_1.Volume; } });
39
+ const events_1 = require("./events");
38
40
  const runtime_1 = require("./runtime");
39
41
  const version_1 = require("./version");
40
42
  const plugins = {
41
43
  App: plugins_1.App,
42
44
  Ota: plugins_1.Ota,
45
+ Migration: plugins_1.Migration,
43
46
  Browser: plugins_1.Browser,
44
47
  Device: plugins_1.Device,
45
48
  Config: plugins_1.Config,
@@ -77,6 +80,8 @@ exports.Native = {
77
80
  isNativePlatform: runtime_1.isNativePlatform,
78
81
  isNativeBridgeAvailable: runtime_1.isNativeBridgeAvailable,
79
82
  whenReady: runtime_1.whenReady,
83
+ /** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
84
+ on: events_1.onNativeEvent,
80
85
  Plugins: plugins,
81
86
  ...plugins,
82
87
  /** H5 自行请求 Client API 时加密请求体(平台公钥来自 App.getInfo().platform.paramPublicKey) */
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Browser = exports.Ota = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.App = void 0;
3
+ exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Browser = exports.Migration = exports.Ota = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.App = void 0;
4
+ const events_1 = require("./events");
4
5
  const runtime_1 = require("./runtime");
5
6
  const webview_signals_1 = require("./webview-signals");
6
7
  function toEnvironmentDevice(device, system) {
@@ -59,6 +60,14 @@ exports.Device = {
59
60
  getOrientation(callback) {
60
61
  return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getOrientation'), callback);
61
62
  },
63
+ /** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
64
+ setOrientation(options, callback) {
65
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setOrientation', { ...options }), callback);
66
+ },
67
+ /** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
68
+ setFullscreen(options, callback) {
69
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setFullscreen', { ...options }), callback);
70
+ },
62
71
  getLockStatus(callback) {
63
72
  return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getLockStatus'), callback);
64
73
  },
@@ -215,6 +224,53 @@ exports.Ota = {
215
224
  downloadAndInstallApk(url, callback) {
216
225
  return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('downloadAndInstallApk', { url }), callback);
217
226
  },
227
+ /**
228
+ * 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
229
+ * 返回取消订阅函数。
230
+ */
231
+ onInstallEvent(handler) {
232
+ return (0, events_1.onNativeEvent)('ota.install', handler);
233
+ },
234
+ /** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
235
+ getInstallState(callback) {
236
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppInstallState'), callback);
237
+ },
238
+ };
239
+ /**
240
+ * 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
241
+ * 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
242
+ * 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
243
+ */
244
+ exports.Migration = {
245
+ /**
246
+ * 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
247
+ * 新应用通过 `Migration.getInfo().extras` 读取。
248
+ */
249
+ setExtras(options, callback) {
250
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setMigrationExtras', {
251
+ extras: options.extras ?? {},
252
+ cookieUrls: options.cookieUrls ?? [],
253
+ }), callback);
254
+ },
255
+ /** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
256
+ continue(callback) {
257
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('continueAppMigration'), callback);
258
+ },
259
+ /** 新应用里读取迁移结果 */
260
+ getInfo(callback) {
261
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getMigrationInfo'), callback);
262
+ },
263
+ /**
264
+ * 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
265
+ * 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
266
+ */
267
+ requestUninstall(target = 'self', callback) {
268
+ return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('requestUninstall', { target }), callback);
269
+ },
270
+ /** 订阅迁移事件。返回取消订阅函数。 */
271
+ onEvent(handler) {
272
+ return (0, events_1.onNativeEvent)('migration', handler);
273
+ },
218
274
  };
219
275
  exports.Browser = {
220
276
  /** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
@@ -31,10 +31,18 @@ function decodeBridgePayload(payload) {
31
31
  }
32
32
  }
33
33
  function installNativeCallback() {
34
- if (typeof window === 'undefined' || window.__nativeBridgeCallback) {
34
+ if (typeof window === 'undefined') {
35
+ return;
36
+ }
37
+ const current = window.__nativeBridgeCallback;
38
+ if (current && current.__sdk) {
35
39
  return;
36
40
  }
37
- window.__nativeBridgeCallback = (id, success, payload) => {
41
+ const previous = current;
42
+ const cb = (id, success, payload) => {
43
+ if (typeof previous === 'function') {
44
+ previous(id, success, payload);
45
+ }
38
46
  const item = pending[id];
39
47
  if (!item) {
40
48
  return;
@@ -48,6 +56,8 @@ function installNativeCallback() {
48
56
  item.reject(new Error(String(data)));
49
57
  }
50
58
  };
59
+ cb.__sdk = true;
60
+ window.__nativeBridgeCallback = cb;
51
61
  }
52
62
  function callNative(method, params) {
53
63
  return new Promise((resolve, reject) => {
@@ -78,6 +88,9 @@ function getPlatform() {
78
88
  if (window.HarmonyBridge?.postMessage) {
79
89
  return 'harmony';
80
90
  }
91
+ if (window.DesktopBridge?.postMessage) {
92
+ return window.DesktopBridge.os === 'macos' ? 'macos' : 'windows';
93
+ }
81
94
  return 'web';
82
95
  }
83
96
  function isNativePlatform() {
@@ -4,4 +4,4 @@ exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
4
4
  /** npm 包名(与 package.json name 保持一致) */
5
5
  exports.PACKAGE_NAME = 'ota-native-bridge';
6
6
  /** OTA Native Bridge SDK 版本(与 package.json version 保持一致) */
7
- exports.SDK_VERSION = '1.6.0';
7
+ exports.SDK_VERSION = '1.8.0';
@@ -0,0 +1,9 @@
1
+ import type { NativeEventMap } from './types';
2
+ /**
3
+ * 订阅壳推送的事件(Android:`ota.install` 下载与安装、`migration` 换包数据迁移)。
4
+ * 返回取消订阅函数。非浏览器环境返回空函数。
5
+ *
6
+ * 页面没加载好时发出的事件不会补发;启动时请用 `Ota.getInstallState()` / `Migration.getInfo()` 读取当前状态。
7
+ */
8
+ export declare function onNativeEvent<K extends keyof NativeEventMap>(name: K, handler: (event: NativeEventMap[K]) => void): () => void;
9
+ export declare function onNativeEvent(name: string, handler: (event: unknown) => void): () => void;
package/dist/events.js ADDED
@@ -0,0 +1,21 @@
1
+ const NATIVE_EVENT = 'nativebridgeevent';
2
+ export function onNativeEvent(name, handler) {
3
+ if (typeof window === 'undefined') {
4
+ return () => undefined;
5
+ }
6
+ const listener = (e) => {
7
+ const detail = e.detail;
8
+ if (!detail || detail.name !== name) {
9
+ return;
10
+ }
11
+ try {
12
+ handler(detail.payload);
13
+ }
14
+ catch (error) {
15
+ // 业务回调异常不要影响后续事件
16
+ console.error('[ota-native-bridge] event handler failed', error);
17
+ }
18
+ };
19
+ window.addEventListener(NATIVE_EVENT, listener);
20
+ return () => window.removeEventListener(NATIVE_EVENT, listener);
21
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './types';
2
2
  export { PACKAGE_NAME, SDK_VERSION } from './version';
3
- export { Native, App, Ota, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
3
+ export { Native, App, Ota, Migration, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
4
+ export { onNativeEvent } from './events';
4
5
  export type { NativeCallback, NativePlatform } from './runtime';
5
6
  export { whenReady, getPlatform, isNativePlatform, invokeNative, decodeBridgePayload } from './runtime';
6
7
  export { getNativePostMessage, isNativeBridgeAvailable, type NativePostMessage, } from './native-messenger';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './types';
2
2
  export { PACKAGE_NAME, SDK_VERSION } from './version';
3
- export { Native, App, Ota, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
3
+ export { Native, App, Ota, Migration, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
4
+ export { onNativeEvent } from './events';
4
5
  export { whenReady, getPlatform, isNativePlatform, invokeNative, decodeBridgePayload } from './runtime';
5
6
  export { getNativePostMessage, isNativeBridgeAvailable, } from './native-messenger';
6
7
  export { encryptRequestBody, postEncrypted, } from './crypto';
@@ -12,6 +12,9 @@ export function getNativePostMessage() {
12
12
  if (window.HarmonyBridge?.postMessage) {
13
13
  return (message) => window.HarmonyBridge.postMessage(message);
14
14
  }
15
+ if (window.DesktopBridge?.postMessage) {
16
+ return (message) => window.DesktopBridge.postMessage(message);
17
+ }
15
18
  return null;
16
19
  }
17
20
  export function isNativeBridgeAvailable() {
package/dist/native.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Crypto } from './crypto-ns';
2
- import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume } from './plugins';
2
+ import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume } from './plugins';
3
+ import { onNativeEvent } from './events';
3
4
  import { getPlatform, isNativeBridgeAvailable, isNativePlatform, whenReady } from './runtime';
4
5
  /** 插件式 Native SDK:直接调用即可,首次会自动等待壳注入完成 */
5
6
  export declare const Native: {
@@ -49,6 +50,15 @@ export declare const Native: {
49
50
  channel: string;
50
51
  }>;
51
52
  downloadAndInstallApk(url: string, callback?: import("./runtime").NativeCallback<import("./types").InstallAppPackageResult>): Promise<import("./types").InstallAppPackageResult>;
53
+ onInstallEvent(handler: (event: import("./types").OtaInstallEvent) => void): () => void;
54
+ getInstallState(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
55
+ };
56
+ readonly Migration: {
57
+ setExtras(options: import("./types").MigrationExtrasOptions, callback?: import("./runtime").NativeCallback<import("./types").NativeOk>): Promise<import("./types").NativeOk>;
58
+ continue(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
59
+ getInfo(callback?: import("./runtime").NativeCallback<import("./types").MigrationInfo>): Promise<import("./types").MigrationInfo>;
60
+ requestUninstall(target?: import("./types").MigrationUninstallTarget, callback?: import("./runtime").NativeCallback<import("./types").MigrationUninstallResult>): Promise<import("./types").MigrationUninstallResult>;
61
+ onEvent(handler: (event: import("./types").MigrationEvent) => void): () => void;
52
62
  };
53
63
  readonly Browser: {
54
64
  open(urlOrOptions: string | import("./types").OpenInBrowserOptions, callback?: import("./runtime").NativeCallback<import("./types").OpenInBrowserResult>): Promise<import("./types").OpenInBrowserResult>;
@@ -57,6 +67,8 @@ export declare const Native: {
57
67
  getInfo(callback?: import("./runtime").NativeCallback<import("./types").DeviceInfo>): Promise<import("./types").DeviceInfo>;
58
68
  getScreenInfo(callback?: import("./runtime").NativeCallback<import("./types").ScreenInfo>): Promise<import("./types").ScreenInfo>;
59
69
  getOrientation(callback?: import("./runtime").NativeCallback<import("./types").OrientationInfo>): Promise<import("./types").OrientationInfo>;
70
+ setOrientation(options: import("./types").SetOrientationOptions, callback?: import("./runtime").NativeCallback<import("./types").SetOrientationResult>): Promise<import("./types").SetOrientationResult>;
71
+ setFullscreen(options: import("./types").SetFullscreenOptions, callback?: import("./runtime").NativeCallback<import("./types").SetFullscreenResult>): Promise<import("./types").SetFullscreenResult>;
60
72
  getLockStatus(callback?: import("./runtime").NativeCallback<import("./types").LockStatus>): Promise<import("./types").LockStatus>;
61
73
  getSystemInfo(callback?: import("./runtime").NativeCallback<import("./types").SystemInfo>): Promise<import("./types").SystemInfo>;
62
74
  getCpuInfo(callback?: import("./runtime").NativeCallback<import("./types").CpuInfo>): Promise<import("./types").CpuInfo>;
@@ -223,11 +235,13 @@ export declare const Native: {
223
235
  readonly Push: {
224
236
  getToken(callback?: import("./runtime").NativeCallback<import("./types").PushTokenResult>): Promise<import("./types").PushTokenResult>;
225
237
  };
226
- readonly sdkVersion: "1.6.0";
238
+ readonly sdkVersion: "1.8.0";
227
239
  readonly getPlatform: typeof getPlatform;
228
240
  readonly isNativePlatform: typeof isNativePlatform;
229
241
  readonly isNativeBridgeAvailable: typeof isNativeBridgeAvailable;
230
242
  readonly whenReady: typeof whenReady;
243
+ /** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
244
+ readonly on: typeof onNativeEvent;
231
245
  readonly Plugins: {
232
246
  readonly App: {
233
247
  ping(callback?: import("./runtime").NativeCallback<{
@@ -270,6 +284,15 @@ export declare const Native: {
270
284
  channel: string;
271
285
  }>;
272
286
  downloadAndInstallApk(url: string, callback?: import("./runtime").NativeCallback<import("./types").InstallAppPackageResult>): Promise<import("./types").InstallAppPackageResult>;
287
+ onInstallEvent(handler: (event: import("./types").OtaInstallEvent) => void): () => void;
288
+ getInstallState(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
289
+ };
290
+ readonly Migration: {
291
+ setExtras(options: import("./types").MigrationExtrasOptions, callback?: import("./runtime").NativeCallback<import("./types").NativeOk>): Promise<import("./types").NativeOk>;
292
+ continue(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
293
+ getInfo(callback?: import("./runtime").NativeCallback<import("./types").MigrationInfo>): Promise<import("./types").MigrationInfo>;
294
+ requestUninstall(target?: import("./types").MigrationUninstallTarget, callback?: import("./runtime").NativeCallback<import("./types").MigrationUninstallResult>): Promise<import("./types").MigrationUninstallResult>;
295
+ onEvent(handler: (event: import("./types").MigrationEvent) => void): () => void;
273
296
  };
274
297
  readonly Browser: {
275
298
  open(urlOrOptions: string | import("./types").OpenInBrowserOptions, callback?: import("./runtime").NativeCallback<import("./types").OpenInBrowserResult>): Promise<import("./types").OpenInBrowserResult>;
@@ -278,6 +301,8 @@ export declare const Native: {
278
301
  getInfo(callback?: import("./runtime").NativeCallback<import("./types").DeviceInfo>): Promise<import("./types").DeviceInfo>;
279
302
  getScreenInfo(callback?: import("./runtime").NativeCallback<import("./types").ScreenInfo>): Promise<import("./types").ScreenInfo>;
280
303
  getOrientation(callback?: import("./runtime").NativeCallback<import("./types").OrientationInfo>): Promise<import("./types").OrientationInfo>;
304
+ setOrientation(options: import("./types").SetOrientationOptions, callback?: import("./runtime").NativeCallback<import("./types").SetOrientationResult>): Promise<import("./types").SetOrientationResult>;
305
+ setFullscreen(options: import("./types").SetFullscreenOptions, callback?: import("./runtime").NativeCallback<import("./types").SetFullscreenResult>): Promise<import("./types").SetFullscreenResult>;
281
306
  getLockStatus(callback?: import("./runtime").NativeCallback<import("./types").LockStatus>): Promise<import("./types").LockStatus>;
282
307
  getSystemInfo(callback?: import("./runtime").NativeCallback<import("./types").SystemInfo>): Promise<import("./types").SystemInfo>;
283
308
  getCpuInfo(callback?: import("./runtime").NativeCallback<import("./types").CpuInfo>): Promise<import("./types").CpuInfo>;
@@ -446,5 +471,5 @@ export declare const Native: {
446
471
  };
447
472
  };
448
473
  };
449
- export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
474
+ export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
450
475
  export { Crypto };
package/dist/native.js CHANGED
@@ -1,10 +1,12 @@
1
1
  import { Crypto } from './crypto-ns';
2
- import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, } from './plugins';
2
+ import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, } from './plugins';
3
+ import { onNativeEvent } from './events';
3
4
  import { getPlatform, isNativeBridgeAvailable, isNativePlatform, whenReady } from './runtime';
4
5
  import { SDK_VERSION } from './version';
5
6
  const plugins = {
6
7
  App,
7
8
  Ota,
9
+ Migration,
8
10
  Browser,
9
11
  Device,
10
12
  Config,
@@ -42,6 +44,8 @@ export const Native = {
42
44
  isNativePlatform,
43
45
  isNativeBridgeAvailable,
44
46
  whenReady,
47
+ /** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
48
+ on: onNativeEvent,
45
49
  Plugins: plugins,
46
50
  ...plugins,
47
51
  /** H5 自行请求 Client API 时加密请求体(平台公钥来自 App.getInfo().platform.paramPublicKey) */
@@ -50,5 +54,5 @@ export const Native = {
50
54
  if (typeof window !== 'undefined') {
51
55
  window.Native = Native;
52
56
  }
53
- export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
57
+ export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
54
58
  export { Crypto };
package/dist/plugins.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type NativeCallback } from './runtime';
2
- import type { AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
2
+ import type { AppInstallState, MigrationEvent, MigrationExtrasOptions, MigrationInfo, MigrationUninstallResult, MigrationUninstallTarget, OtaInstallEvent, AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, SetFullscreenOptions, SetFullscreenResult, SetOrientationOptions, SetOrientationResult, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
3
3
  export declare const App: {
4
4
  ping(callback?: NativeCallback<{
5
5
  pong: boolean;
@@ -19,6 +19,10 @@ export declare const Device: {
19
19
  getInfo(callback?: NativeCallback<DeviceInfo>): Promise<DeviceInfo>;
20
20
  getScreenInfo(callback?: NativeCallback<ScreenInfo>): Promise<ScreenInfo>;
21
21
  getOrientation(callback?: NativeCallback<OrientationInfo>): Promise<OrientationInfo>;
22
+ /** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
23
+ setOrientation(options: SetOrientationOptions, callback?: NativeCallback<SetOrientationResult>): Promise<SetOrientationResult>;
24
+ /** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
25
+ setFullscreen(options: SetFullscreenOptions, callback?: NativeCallback<SetFullscreenResult>): Promise<SetFullscreenResult>;
22
26
  getLockStatus(callback?: NativeCallback<LockStatus>): Promise<LockStatus>;
23
27
  getSystemInfo(callback?: NativeCallback<SystemInfo>): Promise<SystemInfo>;
24
28
  /** 逻辑核心数。Android / iOS 读系统值;Harmony 与浏览器预览读 WebView 的 hardwareConcurrency。 */
@@ -114,6 +118,36 @@ export declare const Ota: {
114
118
  }>;
115
119
  /** 等同 `installAppPackage({ url })`,兼容旧方法名 */
116
120
  downloadAndInstallApk(url: string, callback?: NativeCallback<InstallAppPackageResult>): Promise<InstallAppPackageResult>;
121
+ /**
122
+ * 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
123
+ * 返回取消订阅函数。
124
+ */
125
+ onInstallEvent(handler: (event: OtaInstallEvent) => void): () => void;
126
+ /** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
127
+ getInstallState(callback?: NativeCallback<AppInstallState>): Promise<AppInstallState>;
128
+ };
129
+ /**
130
+ * 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
131
+ * 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
132
+ * 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
133
+ */
134
+ export declare const Migration: {
135
+ /**
136
+ * 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
137
+ * 新应用通过 `Migration.getInfo().extras` 读取。
138
+ */
139
+ setExtras(options: MigrationExtrasOptions, callback?: NativeCallback<NativeOk>): Promise<NativeOk>;
140
+ /** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
141
+ continue(callback?: NativeCallback<AppInstallState>): Promise<AppInstallState>;
142
+ /** 新应用里读取迁移结果 */
143
+ getInfo(callback?: NativeCallback<MigrationInfo>): Promise<MigrationInfo>;
144
+ /**
145
+ * 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
146
+ * 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
147
+ */
148
+ requestUninstall(target?: MigrationUninstallTarget, callback?: NativeCallback<MigrationUninstallResult>): Promise<MigrationUninstallResult>;
149
+ /** 订阅迁移事件。返回取消订阅函数。 */
150
+ onEvent(handler: (event: MigrationEvent) => void): () => void;
117
151
  };
118
152
  export declare const Browser: {
119
153
  /** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
package/dist/plugins.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { onNativeEvent } from './events';
1
2
  import { invokeNative, withCallback } from './runtime';
2
3
  import { readCanvasRenderInfo, readNavigatorCpu, readWebGlInfo } from './webview-signals';
3
4
  function toEnvironmentDevice(device, system) {
@@ -56,6 +57,14 @@ export const Device = {
56
57
  getOrientation(callback) {
57
58
  return withCallback(invokeNative('getOrientation'), callback);
58
59
  },
60
+ /** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
61
+ setOrientation(options, callback) {
62
+ return withCallback(invokeNative('setOrientation', { ...options }), callback);
63
+ },
64
+ /** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
65
+ setFullscreen(options, callback) {
66
+ return withCallback(invokeNative('setFullscreen', { ...options }), callback);
67
+ },
59
68
  getLockStatus(callback) {
60
69
  return withCallback(invokeNative('getLockStatus'), callback);
61
70
  },
@@ -212,6 +221,53 @@ export const Ota = {
212
221
  downloadAndInstallApk(url, callback) {
213
222
  return withCallback(invokeNative('downloadAndInstallApk', { url }), callback);
214
223
  },
224
+ /**
225
+ * 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
226
+ * 返回取消订阅函数。
227
+ */
228
+ onInstallEvent(handler) {
229
+ return onNativeEvent('ota.install', handler);
230
+ },
231
+ /** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
232
+ getInstallState(callback) {
233
+ return withCallback(invokeNative('getAppInstallState'), callback);
234
+ },
235
+ };
236
+ /**
237
+ * 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
238
+ * 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
239
+ * 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
240
+ */
241
+ export const Migration = {
242
+ /**
243
+ * 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
244
+ * 新应用通过 `Migration.getInfo().extras` 读取。
245
+ */
246
+ setExtras(options, callback) {
247
+ return withCallback(invokeNative('setMigrationExtras', {
248
+ extras: options.extras ?? {},
249
+ cookieUrls: options.cookieUrls ?? [],
250
+ }), callback);
251
+ },
252
+ /** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
253
+ continue(callback) {
254
+ return withCallback(invokeNative('continueAppMigration'), callback);
255
+ },
256
+ /** 新应用里读取迁移结果 */
257
+ getInfo(callback) {
258
+ return withCallback(invokeNative('getMigrationInfo'), callback);
259
+ },
260
+ /**
261
+ * 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
262
+ * 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
263
+ */
264
+ requestUninstall(target = 'self', callback) {
265
+ return withCallback(invokeNative('requestUninstall', { target }), callback);
266
+ },
267
+ /** 订阅迁移事件。返回取消订阅函数。 */
268
+ onEvent(handler) {
269
+ return onNativeEvent('migration', handler);
270
+ },
215
271
  };
216
272
  export const Browser = {
217
273
  /** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
package/dist/runtime.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNativeBridgeAvailable } from './native-messenger';
2
- export type NativePlatform = 'android' | 'ios' | 'harmony' | 'web';
2
+ export type NativePlatform = 'android' | 'ios' | 'harmony' | 'windows' | 'macos' | 'web';
3
3
  export type NativeCallback<T> = (result: T, error?: Error) => void;
4
4
  /** 空串 / `"undefined"` 不能 JSON.parse;失败时回传原字符串,避免同步 Uncaught SyntaxError */
5
5
  export declare function decodeBridgePayload(payload: string | object | null | undefined): unknown;
package/dist/runtime.js CHANGED
@@ -20,10 +20,18 @@ export function decodeBridgePayload(payload) {
20
20
  }
21
21
  }
22
22
  function installNativeCallback() {
23
- if (typeof window === 'undefined' || window.__nativeBridgeCallback) {
23
+ if (typeof window === 'undefined') {
24
+ return;
25
+ }
26
+ const current = window.__nativeBridgeCallback;
27
+ if (current && current.__sdk) {
24
28
  return;
25
29
  }
26
- window.__nativeBridgeCallback = (id, success, payload) => {
30
+ const previous = current;
31
+ const cb = (id, success, payload) => {
32
+ if (typeof previous === 'function') {
33
+ previous(id, success, payload);
34
+ }
27
35
  const item = pending[id];
28
36
  if (!item) {
29
37
  return;
@@ -37,6 +45,8 @@ function installNativeCallback() {
37
45
  item.reject(new Error(String(data)));
38
46
  }
39
47
  };
48
+ cb.__sdk = true;
49
+ window.__nativeBridgeCallback = cb;
40
50
  }
41
51
  export function callNative(method, params) {
42
52
  return new Promise((resolve, reject) => {
@@ -67,6 +77,9 @@ export function getPlatform() {
67
77
  if (window.HarmonyBridge?.postMessage) {
68
78
  return 'harmony';
69
79
  }
80
+ if (window.DesktopBridge?.postMessage) {
81
+ return window.DesktopBridge.os === 'macos' ? 'macos' : 'windows';
82
+ }
70
83
  return 'web';
71
84
  }
72
85
  export function isNativePlatform() {