@heybox/hb-sdk 0.7.0-alpha.5 → 0.7.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +17 -16
  3. package/dist/cli-chunks/{build-35mI_IwM.cjs → build-CsXk21Yi.cjs} +2 -2
  4. package/dist/cli-chunks/{context-DysC5qrh.cjs → context-ClUhQYbA.cjs} +1 -1
  5. package/dist/cli-chunks/{create-BJSQzUeZ.cjs → create-vadbYtVj.cjs} +1 -1
  6. package/dist/cli-chunks/{dev-DiGtxt4q.cjs → dev-Dkk4qHsv.cjs} +5 -5
  7. package/dist/cli-chunks/{doctor-DWWVwcd7.cjs → doctor-DzgWYWKU.cjs} +1 -1
  8. package/dist/cli-chunks/{index-k5wJ6TMJ.cjs → index-BMddcNGi.cjs} +1 -1
  9. package/dist/cli-chunks/{index-CgCp6jGG.cjs → index-EYRMHb10.cjs} +14 -14
  10. package/dist/cli-chunks/{login-CzkMdcXM.cjs → login-5TM5_o8q.cjs} +2 -2
  11. package/dist/cli-chunks/{project-vite-TBMlJw8y.cjs → project-vite-eJ89lO8_.cjs} +1 -1
  12. package/dist/cli-chunks/{remote-DE9DIxqw.cjs → remote-DxiHjrI9.cjs} +4 -4
  13. package/dist/cli-chunks/{session--DdaUkcq.cjs → session-DYEm6ALx.cjs} +1 -1
  14. package/dist/cli.cjs +1 -1
  15. package/dist/devtools/mock-host/main.js +43 -23
  16. package/dist/index.cjs.js +108 -68
  17. package/dist/index.esm.js +107 -68
  18. package/dist/protocol.cjs.js +19 -0
  19. package/dist/protocol.esm.js +19 -1
  20. package/dist/templates/vue3-vite-ts/src/App.vue +29 -19
  21. package/dist/templates/vue3-vite-ts/src/__tests__/App.spec.ts +82 -9
  22. package/dist/vite.cjs.js +1 -1
  23. package/dist/vite.esm.js +1 -1
  24. package/package.json +1 -1
  25. package/skill/SKILL.md +8 -7
  26. package/skill/references/api-protocol.md +6 -2
  27. package/skill/references/api-root.md +65 -19
  28. package/skill/references/examples.md +30 -9
  29. package/skill/references/recipes.md +48 -26
  30. package/skill/references/safety-boundaries.md +3 -1
  31. package/skill/references/smoke-evaluation.md +3 -1
  32. package/skill/scripts/sync-references.mjs +31 -9
  33. package/skill/skill.json +4 -4
  34. package/types/core/client.d.ts +15 -9
  35. package/types/core/handshake-state.d.ts +24 -0
  36. package/types/core/sdk.d.ts +6 -17
  37. package/types/core/singleton.d.ts +5 -7
  38. package/types/index.d.ts +5 -3
  39. package/types/protocol/trusted-user-gesture.d.ts +6 -0
  40. package/types/protocol/types.d.ts +1 -1
  41. package/types/protocol.d.ts +1 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0
4
+
5
+ - No user-facing hb-sdk package changes were detected from Conventional Commits.
6
+
7
+ ## 0.7.0-alpha.9
8
+
9
+ ### SDK
10
+
11
+ - 移除旧的公开 readiness 方法;能力调用继续自动等待握手,并新增 `getHandshakeState()` 与 `onHandshakeStateChange()`,供业务查询、立即接收并订阅 `connecting | ready | failed` 持久状态。`ready` 生命周期事件仅保留为不可重放的边沿通知。
12
+
13
+ ### Runtime
14
+
15
+ - 修复授权能力的可信手势归属:Runtime 与浏览器 Mock 复用同一套请求级判定,并按已验证的当前 bridge 请求生成手势快照,避免异步等待、规则漂移或其他窗口操作误用用户手势。
16
+
3
17
  ## 0.7.0-alpha.3
4
18
 
5
19
  ### SDK
package/README.md CHANGED
@@ -23,13 +23,14 @@ npm run dev
23
23
  npm install @heybox/hb-sdk
24
24
  ```
25
25
 
26
- 页面启动时先等待 SDK 就绪。未开通网络权限的小程序可以读取当前小程序内的隔离身份:
26
+ SDK 会在导入时启动握手,能力调用会自动等待 SDK 就绪。未开通网络权限的小程序可以读取当前小程序内的隔离身份:
27
+
28
+ 需要可信用户手势的按钮不能在点击回调里等待握手。页面加载时先用 `getHandshakeState()` 读取当前状态,再用 `onHandshakeStateChange()` 订阅并立即接收当前状态;仅在 `status === 'ready'` 时启用按钮,并在组件或页面销毁时调用订阅返回的取消函数。`ready` 生命周期事件只是握手成功瞬间派发、不会重放的边沿通知,不能用来判断当前状态。
27
29
 
28
30
  ```ts
29
31
  import hbSDK from '@heybox/hb-sdk';
30
32
 
31
33
  async function getLocalIdentityFromUserAction() {
32
- await hbSDK.ready();
33
34
  const { appUserId } = await hbSDK.user.getLocalIdentity();
34
35
  return appUserId;
35
36
  }
@@ -83,27 +84,25 @@ export default defineConfig({
83
84
 
84
85
  ## 错误处理
85
86
 
86
- 初始化或能力调用失败时,SDK 会抛出公开错误:
87
+ 握手失败会保留在持久状态中,可在订阅回放时直接处理原始错误:
87
88
 
88
89
  ```ts
89
- import hbSDK, { HbMiniProgramSDKError } from '@heybox/hb-sdk';
90
-
91
- try {
92
- await hbSDK.ready();
93
- } catch (error) {
94
- if (error instanceof HbMiniProgramSDKError) {
95
- console.error(error.code, error.message);
96
- } else {
97
- throw error;
90
+ import hbSDK from '@heybox/hb-sdk';
91
+
92
+ const stopHandshakeState = hbSDK.onHandshakeStateChange((state) => {
93
+ if (state.status === 'failed') {
94
+ console.error(state.error.code, state.error.message);
98
95
  }
99
- }
96
+ });
97
+
98
+ window.addEventListener('unload', stopHandshakeState, { once: true });
100
99
  ```
101
100
 
102
- 网络状态码校验失败会抛出 `HbMiniProgramNetworkError`。错误字段和处理建议见 [API Reference](https://docs.xiaoheihe.cn/hb_sdk/reference/)。
101
+ 能力调用失败会抛出 `HbMiniProgramSDKError`,网络状态码校验失败会抛出 `HbMiniProgramNetworkError`。错误字段和处理建议见 [API Reference](https://docs.xiaoheihe.cn/hb_sdk/reference/)。
103
102
 
104
103
  ## 能力概览
105
104
 
106
- `ready()` 外,SDK 还提供 `on()` `off()` 处理前后台、登录态等生命周期事件。
105
+ SDK 还提供 `getHandshakeState()` / `onHandshakeStateChange()` 管理持久握手状态,并通过 `on()` / `off()` 处理前后台、登录态等生命周期事件。
107
106
 
108
107
  | 模块 | 用途 |
109
108
  | ------------ | ------------------------------------------------------------ |
@@ -122,7 +121,9 @@ try {
122
121
 
123
122
  ## 关键约束
124
123
 
125
- - `ready()` 只表示 SDK 可调用,不表示已取得用户授权。
124
+ - SDK 会自动等待与父容器完成握手;握手失败会在能力调用时抛出公开 SDK 错误。
125
+ - 交互控件通过 `getHandshakeState()` 和 `onHandshakeStateChange()` 判断是否可用,只在 `status === 'ready'` 时启用;销毁时取消订阅。
126
+ - `ready` 生命周期事件是不可重放的边沿通知,不代表可查询状态,也不能替代握手状态 API。
126
127
  - `auth.login({ scopes? })` 只返回 `{ code, expiresIn: 300, scopes }`;identity 隐式强制包含,code 只能提交给开发者服务端。
127
128
  - 需要授权 UI 时,`auth.login()` 必须来自可信用户手势,否则返回 `USER_GESTURE_REQUIRED`;用户取消或关闭时返回 `AUTHORIZATION_CANCELLED`。已授权时可以静默返回新 code。
128
129
  - `user.getSteamGameList()` 仅供未开通网络权限的小程序通过 Host 读取;已开通网络权限时返回 `SERVER_API_REQUIRED`,且授权码与 OpenAPI 不提供 Steam 游戏库 scope 或资源接口。
@@ -5,8 +5,8 @@ var fs = require('node:fs/promises');
5
5
  var path = require('node:path');
6
6
  var runtimePermissionEnv = require('./runtime-permission-env-D-8_jPG3.cjs');
7
7
  var runtimeGate = require('./runtime-gate-CtV7rWyX.cjs');
8
- var index = require('./index-CgCp6jGG.cjs');
9
- var projectVite = require('./project-vite-TBMlJw8y.cjs');
8
+ var index = require('./index-EYRMHb10.cjs');
9
+ var projectVite = require('./project-vite-eJ89lO8_.cjs');
10
10
  require('node:async_hooks');
11
11
  require('node:module');
12
12
  require('path');
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var session = require('./session--DdaUkcq.cjs');
3
+ var session = require('./session-DYEm6ALx.cjs');
4
4
  var runtimePermissionEnv = require('./runtime-permission-env-D-8_jPG3.cjs');
5
5
  var fs$1 = require('node:fs');
6
6
  var fs = require('node:fs/promises');
@@ -5,7 +5,7 @@ var fs = require('node:fs/promises');
5
5
  var path = require('node:path');
6
6
  var require$$0 = require('fs');
7
7
  var require$$1 = require('path');
8
- var index = require('./index-CgCp6jGG.cjs');
8
+ var index = require('./index-EYRMHb10.cjs');
9
9
  require('node:module');
10
10
  require('os');
11
11
  require('readline');
@@ -6,15 +6,15 @@ var path = require('node:path');
6
6
  var net = require('node:net');
7
7
  var promises = require('node:dns/promises');
8
8
  var node_http = require('node:http');
9
- var context = require('./context-DysC5qrh.cjs');
9
+ var context = require('./context-ClUhQYbA.cjs');
10
10
  var node_crypto = require('node:crypto');
11
11
  var undici = require('undici');
12
12
  var browser = require('./browser-RAy8e8cV.cjs');
13
- var index = require('./index-CgCp6jGG.cjs');
14
- var projectVite = require('./project-vite-TBMlJw8y.cjs');
13
+ var index = require('./index-EYRMHb10.cjs');
14
+ var projectVite = require('./project-vite-eJ89lO8_.cjs');
15
15
  var fs$1 = require('node:fs/promises');
16
16
  var runtimePermissionEnv = require('./runtime-permission-env-D-8_jPG3.cjs');
17
- require('./session--DdaUkcq.cjs');
17
+ require('./session-DYEm6ALx.cjs');
18
18
  require('fs');
19
19
  require('constants');
20
20
  require('stream');
@@ -1347,7 +1347,7 @@ function createHeyboxProtocol(payload) {
1347
1347
  /** 构建时替换为当前发布包的实际版本。 */
1348
1348
  const HB_SDK_VERSION = typeof undefined === 'string'
1349
1349
  ? undefined
1350
- : '0.7.0-alpha.5';
1350
+ : '0.7.0';
1351
1351
 
1352
1352
  class DevPermissionOverrideStoreError extends Error {
1353
1353
  code;
@@ -4,7 +4,7 @@ var fs$1 = require('node:fs');
4
4
  var fs = require('node:fs/promises');
5
5
  var os = require('node:os');
6
6
  var path = require('node:path');
7
- var index = require('./index-CgCp6jGG.cjs');
7
+ var index = require('./index-EYRMHb10.cjs');
8
8
  require('node:module');
9
9
  require('path');
10
10
  require('os');
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index$2 = require('./index-CgCp6jGG.cjs');
3
+ var index$2 = require('./index-EYRMHb10.cjs');
4
4
  var require$$0$2 = require('fs');
5
5
  var require$$2$1 = require('crypto');
6
6
  var require$$1$2 = require('path');
@@ -234,19 +234,19 @@ function requireArgument () {
234
234
 
235
235
  var command = {};
236
236
 
237
- const require$5 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-CgCp6jGG.cjs', document.baseURI).href)));
237
+ const require$5 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-EYRMHb10.cjs', document.baseURI).href)));
238
238
  function __require$4() { return require$5("node:events"); }
239
239
 
240
- const require$4 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-CgCp6jGG.cjs', document.baseURI).href)));
240
+ const require$4 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-EYRMHb10.cjs', document.baseURI).href)));
241
241
  function __require$3() { return require$4("node:child_process"); }
242
242
 
243
- const require$3 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-CgCp6jGG.cjs', document.baseURI).href)));
243
+ const require$3 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-EYRMHb10.cjs', document.baseURI).href)));
244
244
  function __require$2() { return require$3("node:path"); }
245
245
 
246
- const require$2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-CgCp6jGG.cjs', document.baseURI).href)));
246
+ const require$2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-EYRMHb10.cjs', document.baseURI).href)));
247
247
  function __require$1() { return require$2("node:fs"); }
248
248
 
249
- const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-CgCp6jGG.cjs', document.baseURI).href)));
249
+ const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/index-EYRMHb10.cjs', document.baseURI).href)));
250
250
  function __require() { return require$1("node:process"); }
251
251
 
252
252
  var help = {};
@@ -13347,7 +13347,7 @@ function getPathname(pathWithQuery) {
13347
13347
  }
13348
13348
 
13349
13349
  const CLI_VERSION_PLACEHOLDER = ['__HB', 'SDK', 'CLI', 'VERSION__'].join('_');
13350
- const BUILT_CLI_VERSION = '0.7.0-alpha.5';
13350
+ const BUILT_CLI_VERSION = '0.7.0';
13351
13351
  const PACKAGE_JSON_CANDIDATES = [
13352
13352
  path.resolve(__dirname, '..', '..', 'package.json'),
13353
13353
  path.resolve(__dirname, '..', 'package.json'),
@@ -13681,35 +13681,35 @@ function createCommandLoggerResolver(options) {
13681
13681
  };
13682
13682
  }
13683
13683
  const defaultClearLoginStatus = async (...args) => {
13684
- const { clearLoginStatus } = await Promise.resolve().then(function () { return require('./login-CzkMdcXM.cjs'); });
13684
+ const { clearLoginStatus } = await Promise.resolve().then(function () { return require('./login-5TM5_o8q.cjs'); });
13685
13685
  return clearLoginStatus(...args);
13686
13686
  };
13687
13687
  const defaultLoginToHeybox = async (...args) => {
13688
- const { loginToHeybox } = await Promise.resolve().then(function () { return require('./login-CzkMdcXM.cjs'); });
13688
+ const { loginToHeybox } = await Promise.resolve().then(function () { return require('./login-5TM5_o8q.cjs'); });
13689
13689
  return loginToHeybox(...args);
13690
13690
  };
13691
13691
  const defaultPrintLoginStatus = async (...args) => {
13692
- const { printLoginStatus } = await Promise.resolve().then(function () { return require('./login-CzkMdcXM.cjs'); });
13692
+ const { printLoginStatus } = await Promise.resolve().then(function () { return require('./login-5TM5_o8q.cjs'); });
13693
13693
  return printLoginStatus(...args);
13694
13694
  };
13695
13695
  const defaultRunBuildCommand = async (...args) => {
13696
- const { runBuildCommand } = await Promise.resolve().then(function () { return require('./build-35mI_IwM.cjs'); });
13696
+ const { runBuildCommand } = await Promise.resolve().then(function () { return require('./build-CsXk21Yi.cjs'); });
13697
13697
  return runBuildCommand(...args);
13698
13698
  };
13699
13699
  const defaultRunCreateCommand = async (...args) => {
13700
- const { runCreateCommand } = await Promise.resolve().then(function () { return require('./create-BJSQzUeZ.cjs'); });
13700
+ const { runCreateCommand } = await Promise.resolve().then(function () { return require('./create-vadbYtVj.cjs'); });
13701
13701
  return runCreateCommand(...args);
13702
13702
  };
13703
13703
  const defaultRunDevCommand = async (...args) => {
13704
- const { runDevCommand } = await Promise.resolve().then(function () { return require('./dev-DiGtxt4q.cjs'); });
13704
+ const { runDevCommand } = await Promise.resolve().then(function () { return require('./dev-Dkk4qHsv.cjs'); });
13705
13705
  return runDevCommand(...args);
13706
13706
  };
13707
13707
  const defaultRunDoctorCommand = async (...args) => {
13708
- const { runDoctorCommand } = await Promise.resolve().then(function () { return require('./doctor-DWWVwcd7.cjs'); });
13708
+ const { runDoctorCommand } = await Promise.resolve().then(function () { return require('./doctor-DzgWYWKU.cjs'); });
13709
13709
  return runDoctorCommand(...args);
13710
13710
  };
13711
13711
  const defaultRunRemoteCommand = async (...args) => {
13712
- const { runRemoteCommand } = await Promise.resolve().then(function () { return require('./remote-DE9DIxqw.cjs'); });
13712
+ const { runRemoteCommand } = await Promise.resolve().then(function () { return require('./remote-DxiHjrI9.cjs'); });
13713
13713
  return runRemoteCommand(...args);
13714
13714
  };
13715
13715
  function resolveStandaloneLogger(options, verbose) {
@@ -3,9 +3,9 @@
3
3
  var promises = require('node:readline/promises');
4
4
  var node_crypto = require('node:crypto');
5
5
  var node_http = require('node:http');
6
- var session = require('./session--DdaUkcq.cjs');
6
+ var session = require('./session-DYEm6ALx.cjs');
7
7
  var browser = require('./browser-RAy8e8cV.cjs');
8
- var index = require('./index-CgCp6jGG.cjs');
8
+ var index = require('./index-EYRMHb10.cjs');
9
9
  require('node:path');
10
10
  require('fs');
11
11
  require('constants');
@@ -5,7 +5,7 @@ var fs$1 = require('node:fs/promises');
5
5
  var node_module = require('node:module');
6
6
  var path = require('node:path');
7
7
  var node_url = require('node:url');
8
- var index = require('./index-CgCp6jGG.cjs');
8
+ var index = require('./index-EYRMHb10.cjs');
9
9
 
10
10
  function findProjectRoot(startDir) {
11
11
  let current = path.resolve(startDir);
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var promises = require('node:readline/promises');
4
- var index = require('./index-CgCp6jGG.cjs');
5
- var session = require('./session--DdaUkcq.cjs');
4
+ var index = require('./index-EYRMHb10.cjs');
5
+ var session = require('./session-DYEm6ALx.cjs');
6
6
  var childProcess = require('node:child_process');
7
7
  var fs = require('node:fs');
8
8
  var fs$1 = require('node:fs/promises');
@@ -10,7 +10,7 @@ var path = require('node:path');
10
10
  var runtimePermissionEnv = require('./runtime-permission-env-D-8_jPG3.cjs');
11
11
  var runtimeGate = require('./runtime-gate-CtV7rWyX.cjs');
12
12
  var node_crypto = require('node:crypto');
13
- var context = require('./context-DysC5qrh.cjs');
13
+ var context = require('./context-ClUhQYbA.cjs');
14
14
  require('node:module');
15
15
  require('path');
16
16
  require('os');
@@ -271,7 +271,7 @@ function createDefaultCosClient(uploadToken, COS) {
271
271
  };
272
272
  }
273
273
  async function loadCosConstructor() {
274
- const cosModule = await Promise.resolve().then(function () { return require('./index-k5wJ6TMJ.cjs'); }).then(function (n) { return n.index; });
274
+ const cosModule = await Promise.resolve().then(function () { return require('./index-BMddcNGi.cjs'); }).then(function (n) { return n.index; });
275
275
  return cosModule.default;
276
276
  }
277
277
  function formatSize(bytes) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-CgCp6jGG.cjs');
3
+ var index = require('./index-EYRMHb10.cjs');
4
4
  var node_crypto = require('node:crypto');
5
5
  var path = require('node:path');
6
6
  var require$$0$2 = require('fs');
package/dist/cli.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./cli-chunks/index-CgCp6jGG.cjs');
3
+ var index = require('./cli-chunks/index-EYRMHb10.cjs');
4
4
  require('node:module');
5
5
  require('node:fs');
6
6
  require('node:fs/promises');
@@ -7,6 +7,24 @@ const MINI_PROGRAM_BRIDGE_NONCE_PARAM = 'hb_mini_bridge_nonce';
7
7
  /** SDK 向 Runtime 报告 CSP 违规的内部方法名。 */
8
8
  const SDK_CSP_VIOLATION_METHOD = 'sdk.csp.violation';
9
9
 
10
+ function readBrowserEnvironment() {
11
+ try {
12
+ return {
13
+ activeElement: globalThis.document?.activeElement ?? null,
14
+ userActivation: globalThis.navigator?.userActivation,
15
+ };
16
+ }
17
+ catch {
18
+ return { activeElement: null };
19
+ }
20
+ }
21
+ /** 按当前浏览器消息、用户激活和目标 iframe 焦点生成请求级可信手势快照。 */
22
+ function computeTrustedIframeUserGesture(event, iframe, environment = readBrowserEnvironment()) {
23
+ return (event.isTrusted === true
24
+ && environment.userActivation?.isActive === true
25
+ && environment.activeElement === iframe);
26
+ }
27
+
10
28
  const MANAGED_RUNTIME_PERMISSION_KEYS = new Set(['network.request']);
11
29
  /**
12
30
  * 判断 permission key 是否由 Runtime 权限快照管理。
@@ -4084,7 +4102,7 @@ function readLeaderboardCursor(value) {
4084
4102
  }
4085
4103
 
4086
4104
  const VALID_LOGIN_SCOPES = new Set(['identity', 'profile']);
4087
- async function loginMiniProgramRuntime(payload, platformAdapter, identity) {
4105
+ async function loginMiniProgramRuntime(payload, platformAdapter, identity, trustedUserGesture) {
4088
4106
  const scopes = readLoginScopes(payload);
4089
4107
  const miniProgramId = identity?.miniProgramId?.trim();
4090
4108
  if (!miniProgramId) {
@@ -4093,7 +4111,7 @@ async function loginMiniProgramRuntime(payload, platformAdapter, identity) {
4093
4111
  const result = await platformAdapter.auth.login({
4094
4112
  miniProgramId,
4095
4113
  scopes,
4096
- trustedUserGesture: Boolean(await platformAdapter.auth.hasTrustedUserGesture?.()),
4114
+ trustedUserGesture,
4097
4115
  });
4098
4116
  assertAuthLoginResult(result);
4099
4117
  return { code: result.code, expiresIn: result.expiresIn, scopes: [...result.scopes] };
@@ -5177,7 +5195,7 @@ async function getMiniProgramRuntimeUserInfo(platformAdapter, identity) {
5177
5195
  : platformAdapter.user.getUserInfo();
5178
5196
  }
5179
5197
 
5180
- async function getMiniProgramRuntimeLocalIdentity(payload, platformAdapter, identity) {
5198
+ async function getMiniProgramRuntimeLocalIdentity(payload, platformAdapter, identity, trustedUserGesture) {
5181
5199
  if (payload !== undefined) {
5182
5200
  throw createMiniProgramRuntimeBridgeError('INVALID_PARAMS', 'user.getLocalIdentity 不接收入参');
5183
5201
  }
@@ -5187,7 +5205,7 @@ async function getMiniProgramRuntimeLocalIdentity(payload, platformAdapter, iden
5187
5205
  }
5188
5206
  const result = await platformAdapter.user.getLocalIdentity({
5189
5207
  miniProgramId,
5190
- trustedUserGesture: Boolean(await platformAdapter.auth.hasTrustedUserGesture?.()),
5208
+ trustedUserGesture,
5191
5209
  });
5192
5210
  if (!isMiniProgramRuntimeRecord(result)
5193
5211
  || typeof result.appUserId !== 'string'
@@ -5201,7 +5219,7 @@ async function getMiniProgramRuntimeLocalIdentity(payload, platformAdapter, iden
5201
5219
  return { appUserId: result.appUserId };
5202
5220
  }
5203
5221
 
5204
- async function revokeMiniProgramRuntimeAuthorization(payload, platformAdapter, identity) {
5222
+ async function revokeMiniProgramRuntimeAuthorization(payload, platformAdapter, identity, trustedUserGesture) {
5205
5223
  if (payload !== undefined) {
5206
5224
  throw createMiniProgramRuntimeBridgeError('INVALID_PARAMS', 'user.revokeAuthorization 不接收入参');
5207
5225
  }
@@ -5211,7 +5229,7 @@ async function revokeMiniProgramRuntimeAuthorization(payload, platformAdapter, i
5211
5229
  }
5212
5230
  await platformAdapter.user.revokeAuthorization({
5213
5231
  miniProgramId,
5214
- trustedUserGesture: Boolean(await platformAdapter.auth.hasTrustedUserGesture?.()),
5232
+ trustedUserGesture,
5215
5233
  });
5216
5234
  platformAdapter.auth.emitAuthEvent?.('user_info_authorization_change', {
5217
5235
  identity: 'denied',
@@ -5542,15 +5560,16 @@ function isRecord$1(value) {
5542
5560
  return typeof value === 'object' && value !== null && !Array.isArray(value);
5543
5561
  }
5544
5562
 
5563
+ const DEFAULT_MOCK_RUNTIME_REQUEST_CONTEXT = { trustedUserGesture: false };
5545
5564
  const MOCK_DEFAULT_LEADERBOARD_KEY = 'default';
5546
5565
  const MOCK_LEADERBOARD_CACHE_MAX_ENTRIES = 500;
5547
5566
  const MOCK_LEADERBOARD_CURRENT_ENTRY_RANK_COUNT_LIMIT = 5000;
5548
5567
  const MINI_PROGRAM_MOCK_RUNTIME_METHODS = MINI_PROGRAM_PROTOCOL_CAPABILITIES.map((capability) => capability.method);
5549
5568
  const MINI_PROGRAM_MOCK_RUNTIME_METHOD_HANDLER_MAP = {
5550
- [AUTH_LOGIN_METHOD]: (runtime, payload) => runtime.login(payload),
5569
+ [AUTH_LOGIN_METHOD]: (runtime, payload, context) => runtime.login(payload, context),
5551
5570
  [USER_GET_INFO_METHOD]: (runtime) => runtime.getUserInfo(),
5552
- [USER_GET_LOCAL_IDENTITY_METHOD]: (runtime, payload) => runtime.getLocalIdentity(payload),
5553
- [USER_REVOKE_AUTHORIZATION_METHOD]: (runtime, payload) => runtime.revokeAuthorization(payload),
5571
+ [USER_GET_LOCAL_IDENTITY_METHOD]: (runtime, payload, context) => runtime.getLocalIdentity(payload, context),
5572
+ [USER_REVOKE_AUTHORIZATION_METHOD]: (runtime, payload, context) => runtime.revokeAuthorization(payload, context),
5554
5573
  [USER_GET_STEAM_GAME_LIST_METHOD]: (runtime, payload) => runtime.getSteamGameList(payload),
5555
5574
  [SHARE_SHOW_SHARE_MENU_METHOD]: (runtime, payload) => runtime.showShareMenu(payload),
5556
5575
  [SHARE_SCREENSHOT_METHOD]: (runtime, payload) => runtime.shareScreenshot(payload),
@@ -5595,7 +5614,6 @@ function createBrowserMockRuntimePlatformAdapter(options) {
5595
5614
  const user = options.getCurrentUser();
5596
5615
  return user?.app_user_id;
5597
5616
  },
5598
- hasTrustedUserGesture: options.hasTrustedUserGesture,
5599
5617
  async login(request) {
5600
5618
  if (!options.getCurrentUser()) {
5601
5619
  if (!request.trustedUserGesture) {
@@ -5782,7 +5800,7 @@ class MiniProgramMockRuntime {
5782
5800
  platformAdapter: adapter,
5783
5801
  });
5784
5802
  }
5785
- async runMethod(method, payload) {
5803
+ async runMethod(method, payload, context = DEFAULT_MOCK_RUNTIME_REQUEST_CONTEXT) {
5786
5804
  if (!isMiniProgramMockRuntimeMethod(method)) {
5787
5805
  throw createMockBridgeError('METHOD_NOT_FOUND', `未开放小程序能力: ${method}`);
5788
5806
  }
@@ -5797,7 +5815,7 @@ class MiniProgramMockRuntime {
5797
5815
  }
5798
5816
  }
5799
5817
  const handler = MINI_PROGRAM_MOCK_RUNTIME_METHOD_HANDLERS[method];
5800
- return handler(this, payload);
5818
+ return handler(this, payload, context);
5801
5819
  }
5802
5820
  getState() {
5803
5821
  return {
@@ -5810,25 +5828,25 @@ class MiniProgramMockRuntime {
5810
5828
  vibrates: [...this.vibrates],
5811
5829
  };
5812
5830
  }
5813
- login(payload) {
5831
+ login(payload, context) {
5814
5832
  if (!this.authLoginEnabled) {
5815
5833
  throw createMockBridgeError('SERVER_API_REQUIRED', '当前模式要求由小程序服务端完成 auth.login');
5816
5834
  }
5817
- return loginMiniProgramRuntime(payload, this.adapter, this.options.identity);
5835
+ return loginMiniProgramRuntime(payload, this.adapter, this.options.identity, context.trustedUserGesture);
5818
5836
  }
5819
5837
  getUserInfo() {
5820
5838
  this.assertMockHostUserApiEnabled();
5821
5839
  return getMiniProgramRuntimeUserInfo(this.adapter, this.options.identity);
5822
5840
  }
5823
- getLocalIdentity(payload) {
5841
+ getLocalIdentity(payload, context) {
5824
5842
  this.assertMockHostUserApiEnabled();
5825
- return getMiniProgramRuntimeLocalIdentity(payload, this.adapter, this.options.identity);
5843
+ return getMiniProgramRuntimeLocalIdentity(payload, this.adapter, this.options.identity, context.trustedUserGesture);
5826
5844
  }
5827
- revokeAuthorization(payload) {
5845
+ revokeAuthorization(payload, context) {
5828
5846
  if (!this.authorizationMutationEnabled) {
5829
5847
  throw createMockBridgeError('SERVER_API_REQUIRED', '当前模式缺少有效的小程序权限快照');
5830
5848
  }
5831
- return revokeMiniProgramRuntimeAuthorization(payload, this.adapter, this.options.identity);
5849
+ return revokeMiniProgramRuntimeAuthorization(payload, this.adapter, this.options.identity, context.trustedUserGesture);
5832
5850
  }
5833
5851
  async getSteamGameList(payload) {
5834
5852
  this.assertMockHostUserApiEnabled();
@@ -5840,8 +5858,8 @@ class MiniProgramMockRuntime {
5840
5858
  throw createMockBridgeError('SERVER_API_REQUIRED', '当前模式要求由小程序服务端调用用户 API');
5841
5859
  }
5842
5860
  }
5843
- invoke(method, payload) {
5844
- return this.runMethod(method, payload);
5861
+ invoke(method, payload, context = DEFAULT_MOCK_RUNTIME_REQUEST_CONTEXT) {
5862
+ return this.runMethod(method, payload, context);
5845
5863
  }
5846
5864
  showShareMenu(payload) {
5847
5865
  return showMiniProgramRuntimeShareMenu(payload, this.adapter, this.getShareContext());
@@ -7124,7 +7142,9 @@ function handleMessage(event) {
7124
7142
  return;
7125
7143
  }
7126
7144
  if (message.type === 'request') {
7127
- void handleRequest(message);
7145
+ void handleRequest(message, {
7146
+ trustedUserGesture: computeTrustedIframeUserGesture(event, iframe),
7147
+ });
7128
7148
  return;
7129
7149
  }
7130
7150
  if (message.type === 'event' && message.method === SDK_CSP_VIOLATION_METHOD) {
@@ -7148,7 +7168,7 @@ function handleHandshake() {
7148
7168
  });
7149
7169
  pushLog('sdk.handshake', { firstHandshake });
7150
7170
  }
7151
- async function handleRequest(message) {
7171
+ async function handleRequest(message, context) {
7152
7172
  if (!message.id || !message.method) {
7153
7173
  return;
7154
7174
  }
@@ -7157,7 +7177,7 @@ async function handleRequest(message) {
7157
7177
  return;
7158
7178
  }
7159
7179
  try {
7160
- const payload = await runtime.runMethod(message.method, message.payload);
7180
+ const payload = await runtime.runMethod(message.method, message.payload, context);
7161
7181
  postResponse(message, payload);
7162
7182
  pushLog(message.method, createRequestLogDetail(message.method, message.payload, payload));
7163
7183
  }