ota-native-bridge 1.2.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 ADDED
@@ -0,0 +1,76 @@
1
+ # ota-native-bridge
2
+
3
+ 三端 WebView 壳(Android / iOS / HarmonyOS)共用的 Native SDK。
4
+
5
+ **详细文档:** [docs/USAGE.md](./docs/USAGE.md)
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install ota-native-bridge
11
+ ```
12
+
13
+ ## 三行上手
14
+
15
+ ```typescript
16
+ import { Native, App, Ota, Browser, Crypto } from 'ota-native-bridge';
17
+
18
+ if (Native.isNativePlatform()) {
19
+ App.getInfo().then((info) => {
20
+ console.log('版本', info.versionName);
21
+ // 自定义 Client API:info.platform.paramPublicKey + Crypto.postEncrypted
22
+ });
23
+ Ota.check('用户ID');
24
+ Browser.open('https://example.com');
25
+ }
26
+ ```
27
+
28
+ **要点:**
29
+
30
+ - **禁止 `async` / `await`**,用 `.then()` 或 callback;也不必先 `ready()`
31
+ - `Native.isNativePlatform()` 判断是否在壳 App 里
32
+ - 壳内打开外链用 `Browser.open(url)`,不要用 `<a target="_blank">`
33
+ - 自行请求 Client API 时**必须**用 `Crypto.encryptRequestBody` / `postEncrypted`,公钥来自 `App.getInfo().platform.paramPublicKey`
34
+
35
+ ## 常用 API
36
+
37
+ | 你想做什么 | 怎么写 |
38
+ |-----------|--------|
39
+ | 是否在 App 里 | `Native.isNativePlatform()` |
40
+ | 读版本号 | `App.getInfo()` |
41
+ | 读打包渠道码 | `App.getChannelCodes()` |
42
+ | 检查更新 | `Ota.check('用户ID')` |
43
+ | 打开网页 | `Browser.open('https://...')` |
44
+ | 读安全区 | `Viewport.getSafeAreaInsets()` |
45
+ | 原生 HTTP | `Http.request({ url, method, headers })` |
46
+ | 剪贴板 / 分享 | `Clipboard.set('hi')` / `Share.open({ text, url })` |
47
+ | 扫码 / 拍照 | `Scan.scan()` / `Picker.takePhoto()` |
48
+ | 生物识别 | `Biometric.authenticate()` |
49
+ | 支付 / 评分 | `Payment.pay({ url })` / `Store.requestReview()` |
50
+ | 推送 token | `Push.getToken()` |
51
+ | 全屏播视频 | `Media.playVideo({ url, title })` |
52
+ | 读分辨率 / 方向 / 锁屏 | `Device.getScreenInfo()` / `getOrientation()` / `getLockStatus()` |
53
+ | 加密 Client API 请求 | `Crypto.postEncrypted(url, plain, pem)` |
54
+
55
+ ## OTA 服务端地址
56
+
57
+ API 地址写在壳工程 **`OtaConstants`** 源码里,改完需重新编译 App。参数加密公钥写在 `app_config.json` → `platform.paramPublicKey`。
58
+
59
+ | 平台 | 文件 |
60
+ |------|------|
61
+ | Android | `OpenAndroidPackage/.../ota/OtaConstants.kt` |
62
+ | iOS | `OpenIOSPackage/OpenIOS/OTA/OtaConstants.swift` |
63
+ | HarmonyOS | `OpenHarmonyPackage/.../ota/OtaConstants.ets` |
64
+
65
+ ## 本地开发 SDK
66
+
67
+ ```bash
68
+ cd NativeClientSdk
69
+ npm install
70
+ npm run build
71
+ ```
72
+
73
+ ## 相关文档
74
+
75
+ - [USAGE.md](./docs/USAGE.md) — 完整 API 与返回示例
76
+ - 仓库 `docs/CLIENT_API.md` — Client API 与加密信封规范
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Crypto = void 0;
4
+ const crypto_1 = require("./crypto");
5
+ /** 命名空间风格导出,便于 `Crypto.encryptRequestBody(...)` 调用 */
6
+ exports.Crypto = {
7
+ encryptRequestBody: crypto_1.encryptRequestBody,
8
+ postEncrypted: crypto_1.postEncrypted,
9
+ };
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ /**
3
+ * 客户端请求参数混合加密:RSA-OAEP(SHA-256) 包装 AES-256-GCM。
4
+ * 与服务端 PlatformParamCrypto / 壳 OtaRequestCrypto 信封一致。
5
+ * H5 自定义更新等自行请求 Client API 时必须使用本方法加密请求体。
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.encryptRequestBody = encryptRequestBody;
9
+ exports.postEncrypted = postEncrypted;
10
+ function pemToArrayBuffer(pem) {
11
+ const normalized = pem
12
+ .replace(/-----BEGIN PUBLIC KEY-----/g, '')
13
+ .replace(/-----END PUBLIC KEY-----/g, '')
14
+ .replace(/\s+/g, '');
15
+ const binary = atob(normalized);
16
+ const bytes = new Uint8Array(binary.length);
17
+ for (let i = 0; i < binary.length; i++) {
18
+ bytes[i] = binary.charCodeAt(i);
19
+ }
20
+ return bytes.buffer;
21
+ }
22
+ function toBase64(bytes) {
23
+ const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
24
+ let binary = '';
25
+ for (let i = 0; i < view.length; i++) {
26
+ binary += String.fromCharCode(view[i]);
27
+ }
28
+ return btoa(binary);
29
+ }
30
+ function requireSubtle() {
31
+ const subtle = typeof globalThis !== 'undefined'
32
+ ? globalThis.crypto?.subtle
33
+ : undefined;
34
+ if (!subtle) {
35
+ throw new Error('[ota-native-bridge] Web Crypto API unavailable');
36
+ }
37
+ return subtle;
38
+ }
39
+ /**
40
+ * 使用平台公钥加密业务 JSON,返回服务端可解密的信封。
41
+ *
42
+ * @param plain 业务明文(须含 platformCode、apiKey 等)
43
+ * @param publicKeyPem 平台公钥 PEM;可从 `App.getInfo().platform.paramPublicKey` 取得
44
+ * @param platformCode 可选;缺省时取 `plain.platformCode`
45
+ */
46
+ async function encryptRequestBody(plain, publicKeyPem, platformCode) {
47
+ const code = String(platformCode || plain.platformCode || '').trim();
48
+ const pem = (publicKeyPem || '').trim();
49
+ if (!code) {
50
+ throw new Error('[ota-native-bridge] platformCode is required for encryption');
51
+ }
52
+ if (!pem) {
53
+ throw new Error('[ota-native-bridge] paramPublicKey is required for encryption');
54
+ }
55
+ const subtle = requireSubtle();
56
+ const aesKey = await subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt']);
57
+ const iv = new Uint8Array(12);
58
+ crypto.getRandomValues(iv);
59
+ const plainBytes = new TextEncoder().encode(JSON.stringify(plain));
60
+ const cipherBuf = await subtle.encrypt({ name: 'AES-GCM', iv, tagLength: 128 }, aesKey, plainBytes);
61
+ const ct = new Uint8Array(cipherBuf);
62
+ const rawAes = new Uint8Array(await subtle.exportKey('raw', aesKey));
63
+ const rsaKey = await subtle.importKey('spki', pemToArrayBuffer(pem), { name: 'RSA-OAEP', hash: 'SHA-256' }, false, ['encrypt']);
64
+ const wrapped = new Uint8Array(await subtle.encrypt({ name: 'RSA-OAEP' }, rsaKey, rawAes));
65
+ return {
66
+ v: 1,
67
+ platformCode: code,
68
+ ek: toBase64(wrapped),
69
+ iv: toBase64(iv),
70
+ ct: toBase64(ct),
71
+ };
72
+ }
73
+ /**
74
+ * 加密后 POST 到 Client API。自动设置 Content-Type。
75
+ *
76
+ * @returns 服务端 JSON(通常为 `{ code, message, data }`)
77
+ */
78
+ async function postEncrypted(url, plain, publicKeyPem, init) {
79
+ const envelope = await encryptRequestBody(plain, publicKeyPem);
80
+ const response = await fetch(url, {
81
+ ...init,
82
+ method: 'POST',
83
+ headers: {
84
+ 'Content-Type': 'application/json',
85
+ ...(init?.headers || {}),
86
+ },
87
+ body: JSON.stringify(envelope),
88
+ });
89
+ const text = await response.text();
90
+ try {
91
+ return text ? JSON.parse(text) : { code: response.status };
92
+ }
93
+ catch {
94
+ return { code: response.status, message: text };
95
+ }
96
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Crypto = exports.postEncrypted = exports.encryptRequestBody = exports.isNativeBridgeAvailable = exports.getNativePostMessage = 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;
18
+ __exportStar(require("./types"), exports);
19
+ var version_1 = require("./version");
20
+ Object.defineProperty(exports, "PACKAGE_NAME", { enumerable: true, get: function () { return version_1.PACKAGE_NAME; } });
21
+ Object.defineProperty(exports, "SDK_VERSION", { enumerable: true, get: function () { return version_1.SDK_VERSION; } });
22
+ var native_1 = require("./native");
23
+ Object.defineProperty(exports, "Native", { enumerable: true, get: function () { return native_1.Native; } });
24
+ Object.defineProperty(exports, "App", { enumerable: true, get: function () { return native_1.App; } });
25
+ Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return native_1.Ota; } });
26
+ Object.defineProperty(exports, "Browser", { enumerable: true, get: function () { return native_1.Browser; } });
27
+ Object.defineProperty(exports, "Device", { enumerable: true, get: function () { return native_1.Device; } });
28
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return native_1.Config; } });
29
+ Object.defineProperty(exports, "Theme", { enumerable: true, get: function () { return native_1.Theme; } });
30
+ Object.defineProperty(exports, "Icon", { enumerable: true, get: function () { return native_1.Icon; } });
31
+ Object.defineProperty(exports, "Viewport", { enumerable: true, get: function () { return native_1.Viewport; } });
32
+ Object.defineProperty(exports, "Notification", { enumerable: true, get: function () { return native_1.Notification; } });
33
+ Object.defineProperty(exports, "Location", { enumerable: true, get: function () { return native_1.Location; } });
34
+ Object.defineProperty(exports, "Media", { enumerable: true, get: function () { return native_1.Media; } });
35
+ Object.defineProperty(exports, "Http", { enumerable: true, get: function () { return native_1.Http; } });
36
+ Object.defineProperty(exports, "Clipboard", { enumerable: true, get: function () { return native_1.Clipboard; } });
37
+ Object.defineProperty(exports, "Share", { enumerable: true, get: function () { return native_1.Share; } });
38
+ Object.defineProperty(exports, "Picker", { enumerable: true, get: function () { return native_1.Picker; } });
39
+ Object.defineProperty(exports, "Scan", { enumerable: true, get: function () { return native_1.Scan; } });
40
+ Object.defineProperty(exports, "Biometric", { enumerable: true, get: function () { return native_1.Biometric; } });
41
+ Object.defineProperty(exports, "Contacts", { enumerable: true, get: function () { return native_1.Contacts; } });
42
+ Object.defineProperty(exports, "Calendar", { enumerable: true, get: function () { return native_1.Calendar; } });
43
+ Object.defineProperty(exports, "Bluetooth", { enumerable: true, get: function () { return native_1.Bluetooth; } });
44
+ Object.defineProperty(exports, "Nfc", { enumerable: true, get: function () { return native_1.Nfc; } });
45
+ Object.defineProperty(exports, "Sensor", { enumerable: true, get: function () { return native_1.Sensor; } });
46
+ Object.defineProperty(exports, "Haptics", { enumerable: true, get: function () { return native_1.Haptics; } });
47
+ Object.defineProperty(exports, "Flashlight", { enumerable: true, get: function () { return native_1.Flashlight; } });
48
+ Object.defineProperty(exports, "Brightness", { enumerable: true, get: function () { return native_1.Brightness; } });
49
+ Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return native_1.Volume; } });
50
+ Object.defineProperty(exports, "Recorder", { enumerable: true, get: function () { return native_1.Recorder; } });
51
+ Object.defineProperty(exports, "Capture", { enumerable: true, get: function () { return native_1.Capture; } });
52
+ Object.defineProperty(exports, "Payment", { enumerable: true, get: function () { return native_1.Payment; } });
53
+ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return native_1.Store; } });
54
+ Object.defineProperty(exports, "Push", { enumerable: true, get: function () { return native_1.Push; } });
55
+ var runtime_1 = require("./runtime");
56
+ Object.defineProperty(exports, "whenReady", { enumerable: true, get: function () { return runtime_1.whenReady; } });
57
+ Object.defineProperty(exports, "getPlatform", { enumerable: true, get: function () { return runtime_1.getPlatform; } });
58
+ Object.defineProperty(exports, "isNativePlatform", { enumerable: true, get: function () { return runtime_1.isNativePlatform; } });
59
+ Object.defineProperty(exports, "invokeNative", { enumerable: true, get: function () { return runtime_1.invokeNative; } });
60
+ var native_messenger_1 = require("./native-messenger");
61
+ Object.defineProperty(exports, "getNativePostMessage", { enumerable: true, get: function () { return native_messenger_1.getNativePostMessage; } });
62
+ Object.defineProperty(exports, "isNativeBridgeAvailable", { enumerable: true, get: function () { return native_messenger_1.isNativeBridgeAvailable; } });
63
+ var crypto_1 = require("./crypto");
64
+ Object.defineProperty(exports, "encryptRequestBody", { enumerable: true, get: function () { return crypto_1.encryptRequestBody; } });
65
+ Object.defineProperty(exports, "postEncrypted", { enumerable: true, get: function () { return crypto_1.postEncrypted; } });
66
+ var crypto_ns_1 = require("./crypto-ns");
67
+ Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return crypto_ns_1.Crypto; } });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNativePostMessage = getNativePostMessage;
4
+ exports.isNativeBridgeAvailable = isNativeBridgeAvailable;
5
+ function getNativePostMessage() {
6
+ if (typeof window === 'undefined') {
7
+ return null;
8
+ }
9
+ if (window.AndroidBridge?.postMessage) {
10
+ return (message) => window.AndroidBridge.postMessage(message);
11
+ }
12
+ const iosHandler = window.webkit?.messageHandlers?.IOSBridge;
13
+ if (iosHandler?.postMessage) {
14
+ return (message) => iosHandler.postMessage(message);
15
+ }
16
+ if (window.HarmonyBridge?.postMessage) {
17
+ return (message) => window.HarmonyBridge.postMessage(message);
18
+ }
19
+ return null;
20
+ }
21
+ function isNativeBridgeAvailable() {
22
+ return getNativePostMessage() !== null;
23
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
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;
4
+ const crypto_ns_1 = require("./crypto-ns");
5
+ Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return crypto_ns_1.Crypto; } });
6
+ const plugins_1 = require("./plugins");
7
+ Object.defineProperty(exports, "App", { enumerable: true, get: function () { return plugins_1.App; } });
8
+ Object.defineProperty(exports, "Biometric", { enumerable: true, get: function () { return plugins_1.Biometric; } });
9
+ Object.defineProperty(exports, "Bluetooth", { enumerable: true, get: function () { return plugins_1.Bluetooth; } });
10
+ Object.defineProperty(exports, "Brightness", { enumerable: true, get: function () { return plugins_1.Brightness; } });
11
+ Object.defineProperty(exports, "Browser", { enumerable: true, get: function () { return plugins_1.Browser; } });
12
+ Object.defineProperty(exports, "Calendar", { enumerable: true, get: function () { return plugins_1.Calendar; } });
13
+ Object.defineProperty(exports, "Capture", { enumerable: true, get: function () { return plugins_1.Capture; } });
14
+ Object.defineProperty(exports, "Clipboard", { enumerable: true, get: function () { return plugins_1.Clipboard; } });
15
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return plugins_1.Config; } });
16
+ Object.defineProperty(exports, "Contacts", { enumerable: true, get: function () { return plugins_1.Contacts; } });
17
+ Object.defineProperty(exports, "Device", { enumerable: true, get: function () { return plugins_1.Device; } });
18
+ Object.defineProperty(exports, "Flashlight", { enumerable: true, get: function () { return plugins_1.Flashlight; } });
19
+ Object.defineProperty(exports, "Haptics", { enumerable: true, get: function () { return plugins_1.Haptics; } });
20
+ Object.defineProperty(exports, "Http", { enumerable: true, get: function () { return plugins_1.Http; } });
21
+ Object.defineProperty(exports, "Icon", { enumerable: true, get: function () { return plugins_1.Icon; } });
22
+ Object.defineProperty(exports, "Location", { enumerable: true, get: function () { return plugins_1.Location; } });
23
+ Object.defineProperty(exports, "Media", { enumerable: true, get: function () { return plugins_1.Media; } });
24
+ Object.defineProperty(exports, "Nfc", { enumerable: true, get: function () { return plugins_1.Nfc; } });
25
+ Object.defineProperty(exports, "Notification", { enumerable: true, get: function () { return plugins_1.Notification; } });
26
+ Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return plugins_1.Ota; } });
27
+ Object.defineProperty(exports, "Payment", { enumerable: true, get: function () { return plugins_1.Payment; } });
28
+ Object.defineProperty(exports, "Picker", { enumerable: true, get: function () { return plugins_1.Picker; } });
29
+ Object.defineProperty(exports, "Push", { enumerable: true, get: function () { return plugins_1.Push; } });
30
+ Object.defineProperty(exports, "Recorder", { enumerable: true, get: function () { return plugins_1.Recorder; } });
31
+ Object.defineProperty(exports, "Scan", { enumerable: true, get: function () { return plugins_1.Scan; } });
32
+ Object.defineProperty(exports, "Sensor", { enumerable: true, get: function () { return plugins_1.Sensor; } });
33
+ Object.defineProperty(exports, "Share", { enumerable: true, get: function () { return plugins_1.Share; } });
34
+ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return plugins_1.Store; } });
35
+ Object.defineProperty(exports, "Theme", { enumerable: true, get: function () { return plugins_1.Theme; } });
36
+ Object.defineProperty(exports, "Viewport", { enumerable: true, get: function () { return plugins_1.Viewport; } });
37
+ Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return plugins_1.Volume; } });
38
+ const runtime_1 = require("./runtime");
39
+ const version_1 = require("./version");
40
+ const plugins = {
41
+ App: plugins_1.App,
42
+ Ota: plugins_1.Ota,
43
+ Browser: plugins_1.Browser,
44
+ Device: plugins_1.Device,
45
+ Config: plugins_1.Config,
46
+ Theme: plugins_1.Theme,
47
+ Icon: plugins_1.Icon,
48
+ Viewport: plugins_1.Viewport,
49
+ Notification: plugins_1.Notification,
50
+ Location: plugins_1.Location,
51
+ Media: plugins_1.Media,
52
+ Http: plugins_1.Http,
53
+ Clipboard: plugins_1.Clipboard,
54
+ Share: plugins_1.Share,
55
+ Picker: plugins_1.Picker,
56
+ Scan: plugins_1.Scan,
57
+ Biometric: plugins_1.Biometric,
58
+ Contacts: plugins_1.Contacts,
59
+ Calendar: plugins_1.Calendar,
60
+ Bluetooth: plugins_1.Bluetooth,
61
+ Nfc: plugins_1.Nfc,
62
+ Sensor: plugins_1.Sensor,
63
+ Haptics: plugins_1.Haptics,
64
+ Flashlight: plugins_1.Flashlight,
65
+ Brightness: plugins_1.Brightness,
66
+ Volume: plugins_1.Volume,
67
+ Recorder: plugins_1.Recorder,
68
+ Capture: plugins_1.Capture,
69
+ Payment: plugins_1.Payment,
70
+ Store: plugins_1.Store,
71
+ Push: plugins_1.Push,
72
+ };
73
+ /** 插件式 Native SDK:直接调用即可,首次会自动等待壳注入完成 */
74
+ exports.Native = {
75
+ sdkVersion: version_1.SDK_VERSION,
76
+ getPlatform: runtime_1.getPlatform,
77
+ isNativePlatform: runtime_1.isNativePlatform,
78
+ isNativeBridgeAvailable: runtime_1.isNativeBridgeAvailable,
79
+ whenReady: runtime_1.whenReady,
80
+ Plugins: plugins,
81
+ ...plugins,
82
+ /** H5 自行请求 Client API 时加密请求体(平台公钥来自 App.getInfo().platform.paramPublicKey) */
83
+ Crypto: crypto_ns_1.Crypto,
84
+ };
85
+ if (typeof window !== 'undefined') {
86
+ window.Native = exports.Native;
87
+ }