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 +76 -0
- package/dist/cjs/crypto-ns.js +9 -0
- package/dist/cjs/crypto.js +96 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/native-messenger.js +23 -0
- package/dist/cjs/native.js +87 -0
- package/dist/cjs/plugins.js +325 -0
- package/dist/cjs/runtime.js +101 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/version.js +7 -0
- package/dist/crypto-ns.d.ts +6 -0
- package/dist/crypto-ns.js +6 -0
- package/dist/crypto.d.ts +26 -0
- package/dist/crypto.js +92 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/native-messenger.d.ts +6 -0
- package/dist/native-messenger.js +19 -0
- package/dist/native.d.ts +418 -0
- package/dist/native.js +54 -0
- package/dist/plugins.d.ts +209 -0
- package/dist/plugins.js +322 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.js +92 -0
- package/dist/types.d.ts +609 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +4 -0
- package/dist/version.js +4 -0
- package/docs/USAGE.md +1486 -0
- package/ota-native-bridge.d.ts +10 -0
- package/ota-native-bridge.js +399 -0
- package/package.json +75 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"use strict";
|
|
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;
|
|
4
|
+
const runtime_1 = require("./runtime");
|
|
5
|
+
function asBrowserOptions(urlOrOptions) {
|
|
6
|
+
return typeof urlOrOptions === 'string' ? { url: urlOrOptions } : urlOrOptions;
|
|
7
|
+
}
|
|
8
|
+
function asCheckUpdateOptions(uidOrOptions) {
|
|
9
|
+
if (typeof uidOrOptions === 'string') {
|
|
10
|
+
return { uid: uidOrOptions };
|
|
11
|
+
}
|
|
12
|
+
return uidOrOptions ?? {};
|
|
13
|
+
}
|
|
14
|
+
exports.App = {
|
|
15
|
+
ping(callback) {
|
|
16
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('ping'), callback);
|
|
17
|
+
},
|
|
18
|
+
getInfo(callback) {
|
|
19
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppInfo'), callback);
|
|
20
|
+
},
|
|
21
|
+
/** 读取打包写入的前端渠道码 / 统计渠道码 / 广告码 */
|
|
22
|
+
getChannelCodes(callback) {
|
|
23
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppChannelCodes'), callback);
|
|
24
|
+
},
|
|
25
|
+
launch(options, callback) {
|
|
26
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('launchApp', { ...options }), callback);
|
|
27
|
+
},
|
|
28
|
+
isInstalled(packageName, callback) {
|
|
29
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('isAppInstalled', { packageName }), callback);
|
|
30
|
+
},
|
|
31
|
+
checkInstalled(packageNames, callback) {
|
|
32
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('checkAppsInstalled', { packageNames }), callback);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
exports.Device = {
|
|
36
|
+
getInfo(callback) {
|
|
37
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getDeviceInfo'), callback);
|
|
38
|
+
},
|
|
39
|
+
getScreenInfo(callback) {
|
|
40
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getScreenInfo'), callback);
|
|
41
|
+
},
|
|
42
|
+
getOrientation(callback) {
|
|
43
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getOrientation'), callback);
|
|
44
|
+
},
|
|
45
|
+
getLockStatus(callback) {
|
|
46
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getLockStatus'), callback);
|
|
47
|
+
},
|
|
48
|
+
getSystemInfo(callback) {
|
|
49
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getSystemInfo'), callback);
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
exports.Config = {
|
|
53
|
+
getNativeConfig(callback) {
|
|
54
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getNativeConfig'), callback);
|
|
55
|
+
},
|
|
56
|
+
setNativeFeatures(options, callback) {
|
|
57
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setNativeFeatures', { ...options }), callback);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
exports.Theme = {
|
|
61
|
+
get(callback) {
|
|
62
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppTheme'), callback);
|
|
63
|
+
},
|
|
64
|
+
set(options, callback) {
|
|
65
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setAppTheme', { ...options }), callback);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
exports.Icon = {
|
|
69
|
+
list(callback) {
|
|
70
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppIcons'), callback);
|
|
71
|
+
},
|
|
72
|
+
get(callback) {
|
|
73
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppIcon'), callback);
|
|
74
|
+
},
|
|
75
|
+
set(options, callback) {
|
|
76
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setAppIcon', { ...options }), callback);
|
|
77
|
+
},
|
|
78
|
+
setFromUrl(options, callback) {
|
|
79
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setAppIconFromUrl', { ...options }), callback);
|
|
80
|
+
},
|
|
81
|
+
setFromBase64(options, callback) {
|
|
82
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setAppIconFromBase64', { ...options }), callback);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
exports.Viewport = {
|
|
86
|
+
getSafeAreaInsets(callback) {
|
|
87
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getSafeAreaInsets'), callback);
|
|
88
|
+
},
|
|
89
|
+
getWindowInfo(callback) {
|
|
90
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getWindowInfo'), callback);
|
|
91
|
+
},
|
|
92
|
+
refreshSafeArea(callback) {
|
|
93
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('refreshSafeArea'), callback);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
exports.Notification = {
|
|
97
|
+
show(options, callback) {
|
|
98
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('showNotification', { ...options }), callback);
|
|
99
|
+
},
|
|
100
|
+
setBadgeCount(count, callback) {
|
|
101
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setBadgeCount', { count }), callback);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
exports.Ota = {
|
|
105
|
+
checkResourceUpdate(url, callback) {
|
|
106
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('checkResourceUpdate', url ? { url } : {}), callback);
|
|
107
|
+
},
|
|
108
|
+
applyResourceUpdate(url, callback) {
|
|
109
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('applyResourceUpdate', { url }), callback);
|
|
110
|
+
},
|
|
111
|
+
installResourcePackage(options, callback) {
|
|
112
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('installResourcePackage', { ...options }), callback);
|
|
113
|
+
},
|
|
114
|
+
checkApkUpdate(url, callback) {
|
|
115
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('checkApkUpdate', url ? { url } : {}), callback);
|
|
116
|
+
},
|
|
117
|
+
installAppPackage(options, callback) {
|
|
118
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('installAppPackage', { ...options }), callback);
|
|
119
|
+
},
|
|
120
|
+
checkUpdate(uidOrOptions, callback) {
|
|
121
|
+
const options = asCheckUpdateOptions(uidOrOptions);
|
|
122
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('checkAppUpdate', {
|
|
123
|
+
notifyWhenNoUpdate: options.notifyWhenNoUpdate !== false,
|
|
124
|
+
uid: options.uid ?? '',
|
|
125
|
+
}), callback);
|
|
126
|
+
},
|
|
127
|
+
/** 检查更新(推荐):传用户 ID 时会自动 setUid 再检测,一行搞定 */
|
|
128
|
+
check(uid, callback) {
|
|
129
|
+
if (!uid) {
|
|
130
|
+
return exports.Ota.checkUpdate(undefined, callback);
|
|
131
|
+
}
|
|
132
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setOtaUid', { uid }).then(() => (0, runtime_1.invokeNative)('checkAppUpdate', {
|
|
133
|
+
notifyWhenNoUpdate: true,
|
|
134
|
+
uid,
|
|
135
|
+
})), callback);
|
|
136
|
+
},
|
|
137
|
+
setUid(uid, callback) {
|
|
138
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setOtaUid', { uid: uid ?? '' }), callback);
|
|
139
|
+
},
|
|
140
|
+
setChannel(channel, callback) {
|
|
141
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setOtaChannel', { channel: channel ?? '' }), callback);
|
|
142
|
+
},
|
|
143
|
+
/** 等同 `installAppPackage({ url })`,兼容旧方法名 */
|
|
144
|
+
downloadAndInstallApk(url, callback) {
|
|
145
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('downloadAndInstallApk', { url }), callback);
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
exports.Browser = {
|
|
149
|
+
/** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
|
|
150
|
+
open(urlOrOptions, callback) {
|
|
151
|
+
const options = asBrowserOptions(urlOrOptions);
|
|
152
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('openInBrowser', {
|
|
153
|
+
url: options.url,
|
|
154
|
+
uri: options.uri,
|
|
155
|
+
}), callback);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
exports.Location = {
|
|
159
|
+
getStatus(callback) {
|
|
160
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getLocationStatus'), callback);
|
|
161
|
+
},
|
|
162
|
+
requestPermission(callback) {
|
|
163
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('requestLocationPermission'), callback);
|
|
164
|
+
},
|
|
165
|
+
getCurrent(options, callback) {
|
|
166
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getCurrentLocation', { ...(options ?? {}) }), callback);
|
|
167
|
+
},
|
|
168
|
+
startWatch(options, callback) {
|
|
169
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('startLocationWatch', { ...(options ?? {}) }), callback);
|
|
170
|
+
},
|
|
171
|
+
stopWatch(watchId, callback) {
|
|
172
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('stopLocationWatch', { watchId }), callback);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
exports.Media = {
|
|
176
|
+
getVideoPlayerStatus(callback) {
|
|
177
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getVideoPlayerStatus'), callback);
|
|
178
|
+
},
|
|
179
|
+
playVideo(options, callback) {
|
|
180
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('playVideo', { ...options }), callback);
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
exports.Http = {
|
|
184
|
+
/**
|
|
185
|
+
* 原生 OkHttp 请求。Android 8–9 系统 WebView 跨域 XHR 常失败时使用。
|
|
186
|
+
* GET/HEAD 不要带 JSON Content-Type。
|
|
187
|
+
*/
|
|
188
|
+
request(options, callback) {
|
|
189
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('httpRequest', { ...options }), callback);
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
exports.Clipboard = {
|
|
193
|
+
get(callback) {
|
|
194
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('clipboardGet'), callback);
|
|
195
|
+
},
|
|
196
|
+
set(text, callback) {
|
|
197
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('clipboardSet', { text: text ?? '' }), callback);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
exports.Share = {
|
|
201
|
+
open(options, callback) {
|
|
202
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('share', { ...options }), callback);
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
exports.Picker = {
|
|
206
|
+
chooseImage(callback) {
|
|
207
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pickImage'), callback);
|
|
208
|
+
},
|
|
209
|
+
chooseVideo(callback) {
|
|
210
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pickVideo'), callback);
|
|
211
|
+
},
|
|
212
|
+
chooseFile(callback) {
|
|
213
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pickFile'), callback);
|
|
214
|
+
},
|
|
215
|
+
takePhoto(callback) {
|
|
216
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('takePhoto'), callback);
|
|
217
|
+
},
|
|
218
|
+
saveImage(options, callback) {
|
|
219
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('saveImage', { ...options }), callback);
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
exports.Scan = {
|
|
223
|
+
scan(callback) {
|
|
224
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('scanCode'), callback);
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
exports.Biometric = {
|
|
228
|
+
status(callback) {
|
|
229
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('biometricStatus'), callback);
|
|
230
|
+
},
|
|
231
|
+
authenticate(options, callback) {
|
|
232
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('biometricAuth', { ...(options ?? {}) }), callback);
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
exports.Contacts = {
|
|
236
|
+
pick(callback) {
|
|
237
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pickContact'), callback);
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
exports.Calendar = {
|
|
241
|
+
addEvent(options, callback) {
|
|
242
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('addCalendarEvent', { ...(options ?? {}) }), callback);
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
exports.Bluetooth = {
|
|
246
|
+
getState(callback) {
|
|
247
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('bluetoothState'), callback);
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
exports.Nfc = {
|
|
251
|
+
getState(callback) {
|
|
252
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('nfcState'), callback);
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
exports.Sensor = {
|
|
256
|
+
getMotion(callback) {
|
|
257
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('sensorMotion'), callback);
|
|
258
|
+
},
|
|
259
|
+
getPedometer(callback) {
|
|
260
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pedometer'), callback);
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
exports.Haptics = {
|
|
264
|
+
vibrate(ms, callback) {
|
|
265
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('vibrate', { ms: ms ?? 200 }), callback);
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
exports.Flashlight = {
|
|
269
|
+
get(callback) {
|
|
270
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('flashlightGet'), callback);
|
|
271
|
+
},
|
|
272
|
+
set(on, callback) {
|
|
273
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('flashlightSet', { on: !!on }), callback);
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
exports.Brightness = {
|
|
277
|
+
get(callback) {
|
|
278
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('brightnessGet'), callback);
|
|
279
|
+
},
|
|
280
|
+
set(value, callback) {
|
|
281
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('brightnessSet', { value }), callback);
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
exports.Volume = {
|
|
285
|
+
get(callback) {
|
|
286
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('volumeGet'), callback);
|
|
287
|
+
},
|
|
288
|
+
set(value, callback) {
|
|
289
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('volumeSet', { value }), callback);
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
exports.Recorder = {
|
|
293
|
+
start(callback) {
|
|
294
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('recorderStart'), callback);
|
|
295
|
+
},
|
|
296
|
+
stop(callback) {
|
|
297
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('recorderStop'), callback);
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
exports.Capture = {
|
|
301
|
+
screenshot(callback) {
|
|
302
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('screenshot'), callback);
|
|
303
|
+
},
|
|
304
|
+
startScreenRecord(callback) {
|
|
305
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('screenRecordStart'), callback);
|
|
306
|
+
},
|
|
307
|
+
stopScreenRecord(callback) {
|
|
308
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('screenRecordStop'), callback);
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
exports.Payment = {
|
|
312
|
+
pay(options, callback) {
|
|
313
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('paymentPay', { ...options }), callback);
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
exports.Store = {
|
|
317
|
+
requestReview(callback) {
|
|
318
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('storeReview'), callback);
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
exports.Push = {
|
|
322
|
+
getToken(callback) {
|
|
323
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('pushToken'), callback);
|
|
324
|
+
},
|
|
325
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNativeBridgeAvailable = void 0;
|
|
4
|
+
exports.callNative = callNative;
|
|
5
|
+
exports.getPlatform = getPlatform;
|
|
6
|
+
exports.isNativePlatform = isNativePlatform;
|
|
7
|
+
exports.whenReady = whenReady;
|
|
8
|
+
exports.invokeNative = invokeNative;
|
|
9
|
+
exports.withCallback = withCallback;
|
|
10
|
+
const native_messenger_1 = require("./native-messenger");
|
|
11
|
+
Object.defineProperty(exports, "isNativeBridgeAvailable", { enumerable: true, get: function () { return native_messenger_1.isNativeBridgeAvailable; } });
|
|
12
|
+
const BRIDGE_READY_EVENT = 'nativebridgeready';
|
|
13
|
+
let seq = 0;
|
|
14
|
+
const pending = {};
|
|
15
|
+
let readyPromise = null;
|
|
16
|
+
function installNativeCallback() {
|
|
17
|
+
if (typeof window === 'undefined' || window.__nativeBridgeCallback) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
window.__nativeBridgeCallback = (id, success, payload) => {
|
|
21
|
+
const item = pending[id];
|
|
22
|
+
if (!item) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
delete pending[id];
|
|
26
|
+
const data = typeof payload === 'string' ? JSON.parse(payload) : payload;
|
|
27
|
+
if (success) {
|
|
28
|
+
item.resolve(data);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
item.reject(new Error(String(data)));
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function callNative(method, params) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const post = (0, native_messenger_1.getNativePostMessage)();
|
|
38
|
+
if (!post) {
|
|
39
|
+
reject(new Error('Native bridge not available (AndroidBridge / IOSBridge / HarmonyBridge)'));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
installNativeCallback();
|
|
43
|
+
const id = `cb_${++seq}`;
|
|
44
|
+
pending[id] = {
|
|
45
|
+
resolve: resolve,
|
|
46
|
+
reject,
|
|
47
|
+
};
|
|
48
|
+
post(JSON.stringify({ id, method, params: params ?? {} }));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function getPlatform() {
|
|
52
|
+
if (typeof window === 'undefined') {
|
|
53
|
+
return 'web';
|
|
54
|
+
}
|
|
55
|
+
if (window.AndroidBridge?.postMessage) {
|
|
56
|
+
return 'android';
|
|
57
|
+
}
|
|
58
|
+
if (window.webkit?.messageHandlers?.IOSBridge?.postMessage) {
|
|
59
|
+
return 'ios';
|
|
60
|
+
}
|
|
61
|
+
if (window.HarmonyBridge?.postMessage) {
|
|
62
|
+
return 'harmony';
|
|
63
|
+
}
|
|
64
|
+
return 'web';
|
|
65
|
+
}
|
|
66
|
+
function isNativePlatform() {
|
|
67
|
+
return getPlatform() !== 'web';
|
|
68
|
+
}
|
|
69
|
+
function whenReady(timeoutMs = 8000) {
|
|
70
|
+
if (typeof window === 'undefined') {
|
|
71
|
+
return Promise.reject(new Error('Not in browser'));
|
|
72
|
+
}
|
|
73
|
+
if ((0, native_messenger_1.getNativePostMessage)()) {
|
|
74
|
+
return Promise.resolve();
|
|
75
|
+
}
|
|
76
|
+
if (!readyPromise) {
|
|
77
|
+
readyPromise = new Promise((resolve, reject) => {
|
|
78
|
+
const timer = setTimeout(() => {
|
|
79
|
+
readyPromise = null;
|
|
80
|
+
reject(new Error('Bridge timeout'));
|
|
81
|
+
}, timeoutMs);
|
|
82
|
+
const onReady = () => {
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
resolve();
|
|
85
|
+
};
|
|
86
|
+
document.addEventListener(BRIDGE_READY_EVENT, onReady, { once: true });
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return readyPromise;
|
|
90
|
+
}
|
|
91
|
+
function invokeNative(method, params) {
|
|
92
|
+
return whenReady().then(() => callNative(method, params));
|
|
93
|
+
}
|
|
94
|
+
function withCallback(promise, callback) {
|
|
95
|
+
if (typeof callback === 'function') {
|
|
96
|
+
promise
|
|
97
|
+
.then((result) => callback(result))
|
|
98
|
+
.catch((error) => callback(undefined, error instanceof Error ? error : new Error(String(error))));
|
|
99
|
+
}
|
|
100
|
+
return promise;
|
|
101
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
|
|
4
|
+
/** npm 包名(与 package.json name 保持一致) */
|
|
5
|
+
exports.PACKAGE_NAME = 'ota-native-bridge';
|
|
6
|
+
/** OTA Native Bridge SDK 版本(与 package.json version 保持一致) */
|
|
7
|
+
exports.SDK_VERSION = '1.2.0';
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端请求参数混合加密:RSA-OAEP(SHA-256) 包装 AES-256-GCM。
|
|
3
|
+
* 与服务端 PlatformParamCrypto / 壳 OtaRequestCrypto 信封一致。
|
|
4
|
+
* H5 自定义更新等自行请求 Client API 时必须使用本方法加密请求体。
|
|
5
|
+
*/
|
|
6
|
+
export interface EncryptedEnvelope {
|
|
7
|
+
v: number;
|
|
8
|
+
platformCode: string;
|
|
9
|
+
ek: string;
|
|
10
|
+
iv: string;
|
|
11
|
+
ct: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 使用平台公钥加密业务 JSON,返回服务端可解密的信封。
|
|
15
|
+
*
|
|
16
|
+
* @param plain 业务明文(须含 platformCode、apiKey 等)
|
|
17
|
+
* @param publicKeyPem 平台公钥 PEM;可从 `App.getInfo().platform.paramPublicKey` 取得
|
|
18
|
+
* @param platformCode 可选;缺省时取 `plain.platformCode`
|
|
19
|
+
*/
|
|
20
|
+
export declare function encryptRequestBody(plain: Record<string, unknown>, publicKeyPem: string, platformCode?: string): Promise<EncryptedEnvelope>;
|
|
21
|
+
/**
|
|
22
|
+
* 加密后 POST 到 Client API。自动设置 Content-Type。
|
|
23
|
+
*
|
|
24
|
+
* @returns 服务端 JSON(通常为 `{ code, message, data }`)
|
|
25
|
+
*/
|
|
26
|
+
export declare function postEncrypted(url: string, plain: Record<string, unknown>, publicKeyPem: string, init?: Omit<RequestInit, 'body' | 'method'>): Promise<unknown>;
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端请求参数混合加密:RSA-OAEP(SHA-256) 包装 AES-256-GCM。
|
|
3
|
+
* 与服务端 PlatformParamCrypto / 壳 OtaRequestCrypto 信封一致。
|
|
4
|
+
* H5 自定义更新等自行请求 Client API 时必须使用本方法加密请求体。
|
|
5
|
+
*/
|
|
6
|
+
function pemToArrayBuffer(pem) {
|
|
7
|
+
const normalized = pem
|
|
8
|
+
.replace(/-----BEGIN PUBLIC KEY-----/g, '')
|
|
9
|
+
.replace(/-----END PUBLIC KEY-----/g, '')
|
|
10
|
+
.replace(/\s+/g, '');
|
|
11
|
+
const binary = atob(normalized);
|
|
12
|
+
const bytes = new Uint8Array(binary.length);
|
|
13
|
+
for (let i = 0; i < binary.length; i++) {
|
|
14
|
+
bytes[i] = binary.charCodeAt(i);
|
|
15
|
+
}
|
|
16
|
+
return bytes.buffer;
|
|
17
|
+
}
|
|
18
|
+
function toBase64(bytes) {
|
|
19
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
20
|
+
let binary = '';
|
|
21
|
+
for (let i = 0; i < view.length; i++) {
|
|
22
|
+
binary += String.fromCharCode(view[i]);
|
|
23
|
+
}
|
|
24
|
+
return btoa(binary);
|
|
25
|
+
}
|
|
26
|
+
function requireSubtle() {
|
|
27
|
+
const subtle = typeof globalThis !== 'undefined'
|
|
28
|
+
? globalThis.crypto?.subtle
|
|
29
|
+
: undefined;
|
|
30
|
+
if (!subtle) {
|
|
31
|
+
throw new Error('[ota-native-bridge] Web Crypto API unavailable');
|
|
32
|
+
}
|
|
33
|
+
return subtle;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 使用平台公钥加密业务 JSON,返回服务端可解密的信封。
|
|
37
|
+
*
|
|
38
|
+
* @param plain 业务明文(须含 platformCode、apiKey 等)
|
|
39
|
+
* @param publicKeyPem 平台公钥 PEM;可从 `App.getInfo().platform.paramPublicKey` 取得
|
|
40
|
+
* @param platformCode 可选;缺省时取 `plain.platformCode`
|
|
41
|
+
*/
|
|
42
|
+
export async function encryptRequestBody(plain, publicKeyPem, platformCode) {
|
|
43
|
+
const code = String(platformCode || plain.platformCode || '').trim();
|
|
44
|
+
const pem = (publicKeyPem || '').trim();
|
|
45
|
+
if (!code) {
|
|
46
|
+
throw new Error('[ota-native-bridge] platformCode is required for encryption');
|
|
47
|
+
}
|
|
48
|
+
if (!pem) {
|
|
49
|
+
throw new Error('[ota-native-bridge] paramPublicKey is required for encryption');
|
|
50
|
+
}
|
|
51
|
+
const subtle = requireSubtle();
|
|
52
|
+
const aesKey = await subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt']);
|
|
53
|
+
const iv = new Uint8Array(12);
|
|
54
|
+
crypto.getRandomValues(iv);
|
|
55
|
+
const plainBytes = new TextEncoder().encode(JSON.stringify(plain));
|
|
56
|
+
const cipherBuf = await subtle.encrypt({ name: 'AES-GCM', iv, tagLength: 128 }, aesKey, plainBytes);
|
|
57
|
+
const ct = new Uint8Array(cipherBuf);
|
|
58
|
+
const rawAes = new Uint8Array(await subtle.exportKey('raw', aesKey));
|
|
59
|
+
const rsaKey = await subtle.importKey('spki', pemToArrayBuffer(pem), { name: 'RSA-OAEP', hash: 'SHA-256' }, false, ['encrypt']);
|
|
60
|
+
const wrapped = new Uint8Array(await subtle.encrypt({ name: 'RSA-OAEP' }, rsaKey, rawAes));
|
|
61
|
+
return {
|
|
62
|
+
v: 1,
|
|
63
|
+
platformCode: code,
|
|
64
|
+
ek: toBase64(wrapped),
|
|
65
|
+
iv: toBase64(iv),
|
|
66
|
+
ct: toBase64(ct),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 加密后 POST 到 Client API。自动设置 Content-Type。
|
|
71
|
+
*
|
|
72
|
+
* @returns 服务端 JSON(通常为 `{ code, message, data }`)
|
|
73
|
+
*/
|
|
74
|
+
export async function postEncrypted(url, plain, publicKeyPem, init) {
|
|
75
|
+
const envelope = await encryptRequestBody(plain, publicKeyPem);
|
|
76
|
+
const response = await fetch(url, {
|
|
77
|
+
...init,
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
...(init?.headers || {}),
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify(envelope),
|
|
84
|
+
});
|
|
85
|
+
const text = await response.text();
|
|
86
|
+
try {
|
|
87
|
+
return text ? JSON.parse(text) : { code: response.status };
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return { code: response.status, message: text };
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './types';
|
|
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';
|
|
4
|
+
export type { NativeCallback, NativePlatform } from './runtime';
|
|
5
|
+
export { whenReady, getPlatform, isNativePlatform, invokeNative } from './runtime';
|
|
6
|
+
export { getNativePostMessage, isNativeBridgeAvailable, type NativePostMessage, } from './native-messenger';
|
|
7
|
+
export { encryptRequestBody, postEncrypted, type EncryptedEnvelope, } from './crypto';
|
|
8
|
+
export { Crypto } from './crypto-ns';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './types';
|
|
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';
|
|
4
|
+
export { whenReady, getPlatform, isNativePlatform, invokeNative } from './runtime';
|
|
5
|
+
export { getNativePostMessage, isNativeBridgeAvailable, } from './native-messenger';
|
|
6
|
+
export { encryptRequestBody, postEncrypted, } from './crypto';
|
|
7
|
+
export { Crypto } from './crypto-ns';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function getNativePostMessage() {
|
|
2
|
+
if (typeof window === 'undefined') {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
if (window.AndroidBridge?.postMessage) {
|
|
6
|
+
return (message) => window.AndroidBridge.postMessage(message);
|
|
7
|
+
}
|
|
8
|
+
const iosHandler = window.webkit?.messageHandlers?.IOSBridge;
|
|
9
|
+
if (iosHandler?.postMessage) {
|
|
10
|
+
return (message) => iosHandler.postMessage(message);
|
|
11
|
+
}
|
|
12
|
+
if (window.HarmonyBridge?.postMessage) {
|
|
13
|
+
return (message) => window.HarmonyBridge.postMessage(message);
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
export function isNativeBridgeAvailable() {
|
|
18
|
+
return getNativePostMessage() !== null;
|
|
19
|
+
}
|