@skillbuilder/miniapp-simulator 0.0.0-stage → 0.2.1

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 (231) hide show
  1. package/README.md +263 -2
  2. package/assets/phones/Nexus-5X.png +0 -0
  3. package/assets/phones/iPhone-14ProMax.png +0 -0
  4. package/assets/rednote-preview/cover/homebar-1439.jpg +0 -0
  5. package/assets/rednote-preview/cover/homebar-1679.jpg +0 -0
  6. package/assets/rednote-preview/cover/homebar-1919.jpg +0 -0
  7. package/assets/rednote-preview/cover/homebar-2284.jpg +0 -0
  8. package/assets/rednote-preview/cover/homebar-2285.jpg +0 -0
  9. package/assets/rednote-preview/cover/like.png +0 -0
  10. package/assets/rednote-preview/cover/play.png +0 -0
  11. package/assets/rednote-preview/cover/topbar-1439.jpg +0 -0
  12. package/assets/rednote-preview/cover/topbar-1679.jpg +0 -0
  13. package/assets/rednote-preview/cover/topbar-1919.jpg +0 -0
  14. package/assets/rednote-preview/cover/topbar-2284.jpg +0 -0
  15. package/assets/rednote-preview/cover/topbar-2285.jpg +0 -0
  16. package/assets/workbench/index.html +77 -0
  17. package/dist/sw/miniapp-simulator-service-worker.js +971 -0
  18. package/dist/sw/miniapp-simulator-service-worker.min.js +1 -0
  19. package/docs/consumer-guide.md +143 -0
  20. package/docs/electron-review.md +21 -0
  21. package/docs/external-cli-workbench.md +195 -0
  22. package/docs/file-system-design.md +334 -0
  23. package/docs/host-external-api.md +235 -0
  24. package/docs/iframe-service-worker.md +190 -0
  25. package/docs/local-dev.md +106 -0
  26. package/docs/npm-integration-verification.md +35 -0
  27. package/docs/remote-architecture.md +408 -0
  28. package/docs/remote-load-observability.md +268 -0
  29. package/docs/remote-usage.md +376 -0
  30. package/docs/request-design.md +247 -0
  31. package/docs/sdk-usage.md +260 -0
  32. package/docs/server-playground-https-debugging.md +150 -0
  33. package/docs/storage-api-design.md +275 -0
  34. package/docs/tracker.md +339 -0
  35. package/docs/worker-runtime.md +62 -0
  36. package/lib/client-BlQ9j4FW.mjs +775 -0
  37. package/lib/index-DI2oETZ_.mjs +574 -0
  38. package/lib/index.js +10831 -0
  39. package/lib/options-B34ipLmF.mjs +15 -0
  40. package/lib/preload/webview-preload.js +37 -0
  41. package/lib/remote-client.js +11 -0
  42. package/lib/types/business-line.d.ts +7 -0
  43. package/lib/types/capabilities/constants.d.ts +29 -0
  44. package/lib/types/capabilities/csp-snippet.d.ts +34 -0
  45. package/lib/types/capabilities/launch-options.d.ts +7 -0
  46. package/lib/types/capabilities/performance-policy.d.ts +5 -0
  47. package/lib/types/capabilities/security-head-snippet.d.ts +21 -0
  48. package/lib/types/capabilities/security.d.ts +25 -0
  49. package/lib/types/capabilities/style-snippet.d.ts +2 -0
  50. package/lib/types/core/controllers/game-playtest.d.ts +9 -0
  51. package/lib/types/core/controllers/gesture.d.ts +8 -0
  52. package/lib/types/core/controllers/host-api.d.ts +19 -0
  53. package/lib/types/core/controllers/index.d.ts +11 -0
  54. package/lib/types/core/controllers/inspector.d.ts +20 -0
  55. package/lib/types/core/controllers/note-preview.d.ts +9 -0
  56. package/lib/types/core/controllers/permission-sheet.d.ts +7 -0
  57. package/lib/types/core/controllers/widget-cover.d.ts +12 -0
  58. package/lib/types/core/lifecycle.d.ts +7 -0
  59. package/lib/types/core/options.d.ts +33 -0
  60. package/lib/types/core/simulator.d.ts +9 -0
  61. package/lib/types/errors.d.ts +11 -0
  62. package/lib/types/event-bus.d.ts +15 -0
  63. package/lib/types/gesture/host-bus.d.ts +113 -0
  64. package/lib/types/gesture/index.d.ts +27 -0
  65. package/lib/types/gesture/plugins/double-tap.d.ts +10 -0
  66. package/lib/types/gesture/plugins/index.d.ts +27 -0
  67. package/lib/types/gesture/plugins/long-press.d.ts +13 -0
  68. package/lib/types/gesture/plugins/multi-touch-hint.d.ts +11 -0
  69. package/lib/types/gesture/plugins/multi-touch-switch.d.ts +18 -0
  70. package/lib/types/gesture/plugins/selection-disabler.d.ts +11 -0
  71. package/lib/types/gesture/plugins/tap.d.ts +11 -0
  72. package/lib/types/gesture/plugins/touch-emitter.d.ts +11 -0
  73. package/lib/types/gesture/plugins/wheel-scroll.d.ts +11 -0
  74. package/lib/types/gesture/receiver-runtime.d.ts +10 -0
  75. package/lib/types/gesture/receiver-runtime.impl.d.ts +6 -0
  76. package/lib/types/gesture/touch-cursor.d.ts +78 -0
  77. package/lib/types/gesture/touch-dot.d.ts +12 -0
  78. package/lib/types/gesture/types.d.ts +206 -0
  79. package/lib/types/host/shadow-host.d.ts +40 -0
  80. package/lib/types/index.d.ts +56 -0
  81. package/lib/types/injection/guest-log-mark.d.ts +7 -0
  82. package/lib/types/injection/head-builder.d.ts +4 -0
  83. package/lib/types/injection/index.d.ts +19 -0
  84. package/lib/types/injection/modules/console-forward.d.ts +2 -0
  85. package/lib/types/injection/modules/gesture.d.ts +6 -0
  86. package/lib/types/injection/modules/inspector-guest-runtime.d.ts +5 -0
  87. package/lib/types/injection/modules/inspector-pause-runtime.d.ts +6 -0
  88. package/lib/types/injection/modules/inspector.d.ts +8 -0
  89. package/lib/types/injection/modules/jsapi-channel.d.ts +9 -0
  90. package/lib/types/injection/modules/jsapi.d.ts +22 -0
  91. package/lib/types/injection/modules/render-done.d.ts +2 -0
  92. package/lib/types/injection/modules/resource-guard.d.ts +7 -0
  93. package/lib/types/injection/modules/scroll-containment.d.ts +3 -0
  94. package/lib/types/injection/modules/security.d.ts +3 -0
  95. package/lib/types/injection/modules/widget-snapshot.d.ts +13 -0
  96. package/lib/types/injection/preload-builder.d.ts +41 -0
  97. package/lib/types/injection/registry.d.ts +20 -0
  98. package/lib/types/injection/types.d.ts +45 -0
  99. package/lib/types/inspector/host-inspector.d.ts +108 -0
  100. package/lib/types/inspector/index.d.ts +8 -0
  101. package/lib/types/inspector/selector.d.ts +18 -0
  102. package/lib/types/native-ui/devtools/CapabilitiesView.d.ts +18 -0
  103. package/lib/types/native-ui/devtools/DevtoolsPanel.d.ts +24 -0
  104. package/lib/types/native-ui/devtools/LogsView.d.ts +17 -0
  105. package/lib/types/native-ui/devtools/dom.d.ts +40 -0
  106. package/lib/types/native-ui/devtools/index.d.ts +19 -0
  107. package/lib/types/native-ui/devtools/log-entry.d.ts +8 -0
  108. package/lib/types/native-ui/devtools/shortcut.d.ts +12 -0
  109. package/lib/types/native-ui/devtools/tokens.d.ts +14 -0
  110. package/lib/types/native-ui/devtools/types.d.ts +51 -0
  111. package/lib/types/native-ui/index.d.ts +17 -0
  112. package/lib/types/native-ui/note-publish/defaults.d.ts +40 -0
  113. package/lib/types/native-ui/note-publish/index.d.ts +2 -0
  114. package/lib/types/native-ui/note-publish/miniapp-note-publish.d.ts +15 -0
  115. package/lib/types/native-ui/note-publish/note-publish-host.d.ts +15 -0
  116. package/lib/types/native-ui/note-publish/view-data.d.ts +21 -0
  117. package/lib/types/native-ui/permission-sheet/defaults.d.ts +12 -0
  118. package/lib/types/native-ui/permission-sheet/index.d.ts +4 -0
  119. package/lib/types/native-ui/permission-sheet/miniapp-permission-sheet.d.ts +19 -0
  120. package/lib/types/native-ui/permission-sheet/permission-sheet-host.d.ts +23 -0
  121. package/lib/types/native-ui/permission-sheet/types.d.ts +8 -0
  122. package/lib/types/native-ui/phone-vibration.d.ts +10 -0
  123. package/lib/types/native-ui/rednote-preview/chrome-tone.d.ts +44 -0
  124. package/lib/types/native-ui/rednote-preview/defaults.d.ts +30 -0
  125. package/lib/types/native-ui/rednote-preview/half-sheet-drag.d.ts +61 -0
  126. package/lib/types/native-ui/rednote-preview/index.d.ts +2 -0
  127. package/lib/types/native-ui/rednote-preview/register.d.ts +7 -0
  128. package/lib/types/native-ui/rednote-preview/styles.d.ts +4 -0
  129. package/lib/types/native-ui/rednote-preview/xhs-note-detail-shell.d.ts +70 -0
  130. package/lib/types/native-ui/rednote-preview/xhs-note-half-sheet.d.ts +100 -0
  131. package/lib/types/native-ui/rednote-preview/xhs-profile-shell.d.ts +31 -0
  132. package/lib/types/native-ui/toast/frame-toast-host.d.ts +18 -0
  133. package/lib/types/native-ui/toast/index.d.ts +19 -0
  134. package/lib/types/presets.d.ts +11 -0
  135. package/lib/types/remote/client.d.ts +89 -0
  136. package/lib/types/remote/jsapi-config.d.ts +9 -0
  137. package/lib/types/remote/load-timing.d.ts +46 -0
  138. package/lib/types/remote/protocol.d.ts +165 -0
  139. package/lib/types/remote/review-launch.d.ts +6 -0
  140. package/lib/types/remote/runtime-entry.d.ts +1 -0
  141. package/lib/types/remote/runtime.d.ts +14 -0
  142. package/lib/types/remote/simulator-prompt.d.ts +9 -0
  143. package/lib/types/remote/widget-snapshot-guest-runtime.d.ts +1 -0
  144. package/lib/types/remote-client.d.ts +5 -0
  145. package/lib/types/renderer/base.d.ts +8 -0
  146. package/lib/types/renderer/content-host/iframe-content-host.d.ts +84 -0
  147. package/lib/types/renderer/content-host/index.d.ts +3 -0
  148. package/lib/types/renderer/content-host/types.d.ts +89 -0
  149. package/lib/types/renderer/content-host/webview-content-host.d.ts +53 -0
  150. package/lib/types/renderer/iframe.d.ts +93 -0
  151. package/lib/types/renderer/index.d.ts +2 -0
  152. package/lib/types/renderer/rednote-shell.d.ts +73 -0
  153. package/lib/types/renderer/webview.d.ts +59 -0
  154. package/lib/types/runtime/context.d.ts +31 -0
  155. package/lib/types/runtime/jsapi-alert.d.ts +3 -0
  156. package/lib/types/runtime/jsapi-modules/context.d.ts +21 -0
  157. package/lib/types/runtime/jsapi-modules/filesystem.d.ts +19 -0
  158. package/lib/types/runtime/jsapi-modules/index.d.ts +9 -0
  159. package/lib/types/runtime/jsapi-modules/interaction.d.ts +4 -0
  160. package/lib/types/runtime/jsapi-modules/location.d.ts +3 -0
  161. package/lib/types/runtime/jsapi-modules/media.d.ts +13 -0
  162. package/lib/types/runtime/jsapi-modules/network.d.ts +3 -0
  163. package/lib/types/runtime/jsapi-modules/storage.d.ts +26 -0
  164. package/lib/types/runtime/jsapi-modules/system.d.ts +4 -0
  165. package/lib/types/runtime/jsapi-modules/types.d.ts +21 -0
  166. package/lib/types/runtime/jsapi-modules/window-info.d.ts +43 -0
  167. package/lib/types/runtime/request/host-request.d.ts +13 -0
  168. package/lib/types/runtime/request/request-manager-frame.d.ts +29 -0
  169. package/lib/types/runtime/request/request-runtime.d.ts +26 -0
  170. package/lib/types/runtime/request/request-types.d.ts +26 -0
  171. package/lib/types/runtime/request/sse-text-framer.d.ts +16 -0
  172. package/lib/types/seal-config.d.ts +134 -0
  173. package/lib/types/sw/index.d.ts +9 -0
  174. package/lib/types/sw/parse-raw.d.ts +34 -0
  175. package/lib/types/sw/protocol.d.ts +19 -0
  176. package/lib/types/sw/served-entry.d.ts +9 -0
  177. package/lib/types/sw/service-worker-content-host.d.ts +11 -0
  178. package/lib/types/sw/service-worker-host.d.ts +74 -0
  179. package/lib/types/sw/service-worker-renderer.d.ts +10 -0
  180. package/lib/types/sw/worker/fs-codec.d.ts +23 -0
  181. package/lib/types/sw/worker/fs-ops.d.ts +22 -0
  182. package/lib/types/sw/worker/fs-store.d.ts +29 -0
  183. package/lib/types/sw/worker/globals.d.ts +50 -0
  184. package/lib/types/sw/worker/main.d.ts +6 -0
  185. package/lib/types/sw/worker/mime.d.ts +2 -0
  186. package/lib/types/sw/worker/mount-store.d.ts +48 -0
  187. package/lib/types/sw/worker/paths.d.ts +4 -0
  188. package/lib/types/sw/worker/serve.d.ts +13 -0
  189. package/lib/types/tracker/constants.d.ts +14 -0
  190. package/lib/types/tracker/default-tracker.d.ts +19 -0
  191. package/lib/types/tracker/index.d.ts +70 -0
  192. package/lib/types/transport/bridge-protocol.d.ts +78 -0
  193. package/lib/types/transport/iframe-transport.d.ts +39 -0
  194. package/lib/types/transport/index.d.ts +14 -0
  195. package/lib/types/transport/jsapi-bridge-messages.d.ts +36 -0
  196. package/lib/types/transport/types.d.ts +48 -0
  197. package/lib/types/transport/webview-transport.d.ts +52 -0
  198. package/lib/types/types/events.d.ts +130 -0
  199. package/lib/types/types/index.d.ts +11 -0
  200. package/lib/types/types/inspector.d.ts +172 -0
  201. package/lib/types/types/jsapi.d.ts +18 -0
  202. package/lib/types/types/options.d.ts +851 -0
  203. package/lib/types/types/renderer.d.ts +145 -0
  204. package/lib/types/utils/ancestor-scroll.d.ts +11 -0
  205. package/lib/types/utils/error-svg.d.ts +12 -0
  206. package/lib/types/utils/frameless-shell.d.ts +114 -0
  207. package/lib/types/utils/miniapp-chrome-svg.d.ts +47 -0
  208. package/lib/types/utils/package-constants.d.ts +8 -0
  209. package/lib/types/utils/screenshot.d.ts +14 -0
  210. package/lib/types/utils/simulator-pc-guard.d.ts +10 -0
  211. package/lib/types/utils/stage-fit.d.ts +6 -0
  212. package/lib/types/utils/touch-emulate.d.ts +26 -0
  213. package/lib/types/utils/zip-entry.d.ts +13 -0
  214. package/lib/types/workbench/index.d.ts +63 -0
  215. package/lib/types/workbench/preview-transition.d.ts +8 -0
  216. package/lib/types/workbench/server/artifact.d.ts +8 -0
  217. package/lib/types/workbench/server/index.d.ts +19 -0
  218. package/lib/types/workbench/server/watch.d.ts +6 -0
  219. package/lib/types/workbench/view.d.ts +36 -0
  220. package/lib/types/worker/adapters/csp-policy.d.ts +7 -0
  221. package/lib/types/worker/adapters/host-lifecycle.d.ts +5 -0
  222. package/lib/types/worker/adapters/mount-recovery.d.ts +2 -0
  223. package/lib/types/worker/adapters/package-policy.d.ts +12 -0
  224. package/lib/types/worker/adapters/page-lifecycle.d.ts +1 -0
  225. package/lib/types/worker/adapters/protocol.d.ts +49 -0
  226. package/lib/types/worker/adapters/scripts.d.ts +2 -0
  227. package/lib/types/worker/adapters/service-worker.d.ts +2 -0
  228. package/lib/workbench-server.mjs +183 -0
  229. package/lib/workbench.js +1404 -0
  230. package/package.json +61 -4
  231. package/scripts/mock-cli-dev.mjs +52 -0
@@ -0,0 +1,339 @@
1
+ # Tracker 上报文档
2
+
3
+ ## 概述
4
+
5
+ miniapp-simulator SDK 内置生命周期 Tracker,监控模拟器从初始化到销毁的全链路关键节点,并上报 bridge 调用情况。
6
+
7
+ ## APM 应用信息
8
+
9
+ | 字段 | 值 |
10
+ | ------------------------------ | ------------------------- |
11
+ | 应用名(context_artifactName) | `skillbuilder-miniapp` |
12
+ | 点位名(measurement_name) | `miniapp_simulator_event` |
13
+
14
+ ## 接入方式
15
+
16
+ 内置默认上报直接把自定义打点 POST 到 APM 端点,接入方无需额外安装。
17
+
18
+ ```typescript
19
+ // 内置默认上报 —— business_line 必填
20
+ createMiniAppSimulator({ tracker: { business_line: 'creator' } });
21
+
22
+ // 上报到海外(rednote)
23
+ createMiniAppSimulator({ tracker: { business_line: 'creator', region: 'sg' } });
24
+
25
+ // 覆盖上报端点(可选,优先级高于 region)
26
+ createMiniAppSimulator({
27
+ tracker: { business_line: 'creator', endpoint: 'https://your-apm/api/data' },
28
+ });
29
+
30
+ // 不启用(默认)
31
+ // tracker 不传或传 false
32
+ ```
33
+
34
+ > - `business_line` 取值 `creator`(创作平台)/ `audit`(审核平台),作为公共字段注入每条打点(缺省兜底 `unknown`)。
35
+ > - 如需上报用户 ID,可在 tracker 选项里提供 `getUserInfo`(见下文「默认上报说明」)。
36
+ > - `region`:`cn`(默认)/ `sg`(海外)。端点只按当前 hostname 选择:`localhost` / `127.0.0.1` / `0.0.0.0` / 包含 `.sit.` / 以 `local.` 开头走测试 APM;`.beta.` 和其他域名继续上报正式 APM。
37
+ > - `endpoint`:直接指定上报 URL,传了则忽略 `region`。
38
+ > - `debug`:打印到 console 而非真正上报;仅显式传 `true` 时开启。
39
+
40
+ ### 默认上报说明
41
+
42
+ - **`getUserInfo` 取用户 ID**:在 tracker 选项里提供 `getUserInfo()`,SDK 上报时会调用它取
43
+ `userId` 作为 APM 用户标识;可同步或异步返回,未提供 / 返回空则 `userId` 留空,不影响其它字段。
44
+
45
+ ```typescript
46
+ createMiniAppSimulator({
47
+ tracker: {
48
+ business_line: 'creator',
49
+ async getUserInfo() {
50
+ return { userId: 'current-user-id' };
51
+ },
52
+ },
53
+ });
54
+ ```
55
+
56
+ - **批量上报**:打点先进缓冲,满 10 条或最多 10s 后合并成一次请求发送;页面隐藏(`visibilitychange`)
57
+ 与 `destroy()` 时立即冲刷,避免关闭前丢数据。
58
+ - **本地开发**:`localhost` / `127.0.0.1` 默认上报到测试 APM;需要只看 console 时显式传 `debug: true`。
59
+
60
+ ## 字段说明
61
+
62
+ ### 公共字段
63
+
64
+ 每条打点自动注入,无需各事件单独声明。
65
+
66
+ | 字段 | 类型 | 说明 |
67
+ | --------------- | -------- | --------------------------------------------------------------------- |
68
+ | `event_name` | `string` | 事件名称 |
69
+ | `timestamp` | `number` | 打点时间戳(ms) |
70
+ | `business_line` | `string` | 业务线:区分使用模拟器的平台(`creator` / `audit`,默认上报模式必填) |
71
+ | `sdk_version` | `string` | SDK 版本号 |
72
+ | `content_mode` | `string` | 内容模式:`half` / `fullscreen` |
73
+ | `renderer` | `string` | 渲染器类型:`iframe-service-worker` / `webview` |
74
+
75
+ ### 事件字段
76
+
77
+ 同一字段在不同 `event_name` 下含义不同。
78
+
79
+ | 字段 | 类型 | 说明 |
80
+ | ------------ | -------- | -------------------------------------------------------------------------------------- |
81
+ | `url` | `string` | 相关 URL |
82
+ | `source` | `string` | 来源(触发来源 / 导航来源 URL) |
83
+ | `duration` | `number` | 耗时(ms) |
84
+ | `message` | `string` | 文本信息(错误信息 / 销毁原因等) |
85
+ | `error_name` | `string` | 标识名(错误码 / bridge 事件名) |
86
+ | `result` | `string` | 操作结果(`success` / `fail` / `allow` / `deny` / `cancel` / `injected` / `fallback`) |
87
+ | `type` | `string` | 类型标识(`zip` / `files` / 权限种类) |
88
+ | `count` | `number` | 数量 |
89
+ | `entry` | `string` | 入口文件名 |
90
+
91
+ ## 上报事件清单
92
+
93
+ ### 维护范围
94
+
95
+ | 分类 | 事件 | 策略 |
96
+ | --- | --- | --- |
97
+ | 核心加载 | `simulator_load_stage`、`simulator_before_load`、`simulator_ready`、`simulator_load`、`simulator_load_error`、`simulator_render_done`、`simulator_parse_done`、`simulator_sw_register`、`simulator_sw_mount` | 持续维护,用于加载耗时与成功率看板 |
98
+ | 故障上下文 | `simulator_guest_error`、`simulator_inject_done` | 持续维护,用于解释加载失败、白屏和运行异常 |
99
+ | 非加载生命周期 | `simulator_init`、`simulator_reload`、`simulator_navigation_blocked`、`simulator_permission_result`、`simulator_destroy`、`bridge_invoke` | 保留已有上报兼容性,不纳入核心加载口径,不继续扩展字段 |
100
+
101
+ 历史事件在确认无下游查询和告警依赖前不直接删除。核心加载看板只依赖前两类,避免把人工操作和普通
102
+ JSAPI 调用耗时误判为模拟器加载耗时。
103
+
104
+ ### simulator_init
105
+
106
+ 模拟器实例构造完成时触发。
107
+
108
+ | 字段 | 值 |
109
+ | ----- | -------------- |
110
+ | `url` | 小应用入口 URL |
111
+
112
+ ### simulator_before_load
113
+
114
+ `load()` 触发前发出。
115
+
116
+ | 字段 | 值 |
117
+ | -------- | ----------------------------------------------------------------------------------------------------- |
118
+ | `url` | 即将加载的 URL |
119
+ | `source` | 触发来源:`load` / `reload` / `switch-mode` / `switch-frame` / `load-app` / `load-zip` / `load-files` |
120
+
121
+ ### simulator_ready
122
+
123
+ 渲染容器 mount 完成时触发。仅公共字段。
124
+
125
+ ### simulator_load
126
+
127
+ 内容加载完成时触发。
128
+
129
+ | 字段 | 值 |
130
+ | ---------- | -------------- |
131
+ | `url` | 加载的 URL |
132
+ | `duration` | 加载耗时(ms) |
133
+
134
+ ### simulator_load_error
135
+
136
+ 加载失败时触发。
137
+
138
+ | 字段 | 值 |
139
+ | ------------ | ------------------------------------------------------------ |
140
+ | `url` | 失败的 URL |
141
+ | `error_name` | 错误码:`LOAD_TIMEOUT` / `LOAD_FAILED` / `RENDERER_ERROR` 等 |
142
+ | `message` | 错误信息 |
143
+
144
+ ### simulator_render_done
145
+
146
+ 子文档首帧渲染完毕时触发。
147
+
148
+ | 字段 | 值 |
149
+ | ---------- | ------------------ |
150
+ | `url` | 完成渲染的 URL |
151
+ | `duration` | 首帧渲染耗时(ms) |
152
+
153
+ ### simulator_reload
154
+
155
+ 调用 `simulator.reload()` 时触发。
156
+
157
+ | 字段 | 值 |
158
+ | ----- | -------------- |
159
+ | `url` | 重新加载的 URL |
160
+
161
+ ### simulator_inject_done
162
+
163
+ head 注入完成时触发。
164
+
165
+ | 字段 | 值 |
166
+ | ---------- | ------------------------------------------------------- |
167
+ | `url` | 注入的 URL |
168
+ | `result` | `injected`(走注入路径)/ `fallback`(降级为 src 直连) |
169
+ | `duration` | 注入耗时(ms) |
170
+
171
+ ### simulator_navigation_blocked
172
+
173
+ iframe/webview 内部导航被拦截时触发。
174
+
175
+ | 字段 | 值 |
176
+ | -------- | -------- |
177
+ | `source` | 来源 URL |
178
+ | `url` | 目标 URL |
179
+
180
+ ### simulator_guest_error
181
+
182
+ 小工具内部发生未捕获错误时触发。
183
+
184
+ | 字段 | 值 |
185
+ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
186
+ | `type` | 监听的事件名:`error`(JS 异常 + 资源加载失败)/ `unhandledrejection`(Promise 未处理拒绝)/ `securitypolicyviolation`(CSP 拦截)/ `fetch`(网络失败) |
187
+ | `error_name` | 错误名(JS:error.name;资源:`ResourceError`;CSP:`CSPViolation`;fetch:error.name) |
188
+ | `message` | JS / Promise 为错误堆栈,其余为错误消息(截断至 1000 字符);CSP 为被拦截的指令 |
189
+ | `source` | JS:`filename:line:col`;资源:标签名;CSP:`sourceFile:line:col` |
190
+ | `url` | 出错脚本 / 失败资源 / 被拦截资源的 URL |
191
+ | `disposition` | CSP 处置方式:`enforce`(拦截)/ `report`(仅上报)。仅 CSP 有 |
192
+
193
+ ### simulator_parse_done
194
+
195
+ `loadZip()` / `loadFiles()` 解析完成时触发。
196
+
197
+ | 字段 | 值 |
198
+ | ---------- | --------------- |
199
+ | `type` | `zip` / `files` |
200
+ | `duration` | 解析耗时(ms) |
201
+ | `count` | 文件数量 |
202
+ | `entry` | 入口文件名 |
203
+
204
+ ### simulator_permission_result
205
+
206
+ 权限弹窗操作完成时触发。
207
+
208
+ | 字段 | 值 |
209
+ | -------- | --------------------------- |
210
+ | `type` | 权限种类(逗号分隔) |
211
+ | `result` | `allow` / `deny` / `cancel` |
212
+
213
+ ### simulator_destroy
214
+
215
+ 模拟器销毁时触发。
216
+
217
+ | 字段 | 值 |
218
+ | --------- | -------- |
219
+ | `message` | 销毁原因 |
220
+
221
+ ### bridge_invoke
222
+
223
+ bridge(jsapi)调用完成时触发。
224
+
225
+ | 字段 | 值 |
226
+ | ------------ | ------------------------------- |
227
+ | `error_name` | bridge 事件名(如 `showToast`) |
228
+ | `result` | `success` / `fail` |
229
+ | `duration` | 成功时的执行耗时(ms) |
230
+ | `message` | 失败时的错误原因 |
231
+
232
+ ## 核心加载链路
233
+
234
+ 核心诊断统一使用 `simulator_load_stage`,阶段差异放在 `stage`,不为每个阶段新增点位或字段。
235
+ Client、Runtime 和 Core 复用远程协议中已有的 `sessionId`,上报字段统一命名为 `session_id`;
236
+ 上报不经过 MessageChannel,不增加协议字段。
237
+
238
+ ### 字段登记
239
+
240
+ | 字段 | 类型 | 说明 |
241
+ | ----------------- | -------- | --------------------------------------------------------- |
242
+ | `session_id` | `string` | 一次远程模拟器连接,全链路关联键 |
243
+ | `app_id` | `string` | 小工具 ID |
244
+ | `runtime_version` | `string` | Runtime 发布版本 |
245
+ | `stage` | `string` | 低基数加载阶段 |
246
+ | `result` | `string` | `success` / `fail` / `timeout` / `fallback` / `protocol_mismatch` |
247
+ | `duration` | `number` | 当前阶段耗时,单位 ms |
248
+ | `type` | `string` | 通用分类,如 `zip` / `files` / `primary` / `fallback` |
249
+ | `count` | `number` | 文件或资源数量 |
250
+ | `bytes` | `number` | 包或传输体积,单位 byte |
251
+ | `network_duration` | `number` | 网络资源总耗时,单位 ms |
252
+ | `dns_duration` | `number` | DNS 耗时,单位 ms |
253
+ | `connect_duration` | `number` | TCP/TLS 建连耗时,单位 ms |
254
+ | `ttfb_duration` | `number` | 请求发出到首字节耗时,单位 ms |
255
+ | `code` | `number` | HTTP 状态码 |
256
+ | `error_name` | `string` | 字符串业务错误码或网络错误分类 |
257
+ | `message` | `string` | 截断后的错误信息,仅失败时上报 |
258
+
259
+ `business_line`、`sdk_version` 和 `renderer` 继续复用公共字段。网络字段为跨请求类型通用字段,
260
+ 同样用于 manifest、Runtime 文档、Runtime 脚本和动态策略请求。
261
+ `request_id` 复用已有字段,不需要新增登记:`session_id` 关联一次 Runtime 连接,`request_id`
262
+ 关联该连接中的一次 `loadZip` / `loadFiles`,从 Client 资源物化一直串到 Core 和 Guest。
263
+ `loadZip` / `loadFiles` 是核心加载入口;其他远程方法仍上报通用调用耗时,但不计入核心加载
264
+ 成功率、耗时分位和阶段漏斗。
265
+ 下载耗时不单独登记字段,统一使用 `<stage>_download` 阶段的 `duration`,例如
266
+ `runtime_manifest_download`、`runtime_document_download` 和 `runtime_config_download`。
267
+
268
+ 当前 Runtime 与 Service Worker 都是单文件 bundle。模拟器自身资源由 `runtime_manifest`、
269
+ `runtime_document`、`runtime_script`、`runtime_config` 和 `simulator_sw_register` 覆盖;其中
270
+ `simulator_sw_register` 是 SW bootstrap、CDN SW 模块加载、安装和激活的端到端耗时。
271
+ `request-manager` 按业务网络请求懒加载,不属于模拟器首次 ready 的关键路径。
272
+
273
+ ### 阶段清单
274
+
275
+ | 阶段 | 诊断目标 |
276
+ | ------------------------------------- | -------------------------------------------- |
277
+ | `runtime_manifest` | Runtime 清单请求总耗时和主/备用服务结果 |
278
+ | `runtime_document` | Client 观察到的 Runtime iframe 总加载耗时:导航到 `load`,包含文档、模块依赖和执行 |
279
+ | `runtime_bootstrap` | Runtime 页面启动时采集的导航、模块执行和 DOM 初始化耗时,只采一次 |
280
+ | `runtime_script` | Runtime 模块脚本网络分段 |
281
+ | `<stage>_download` | 对应网络阶段的响应体下载耗时 |
282
+ | `runtime_handshake` | connect 到 ready |
283
+ | `runtime_config` | 动态策略请求总耗时和网络分段 |
284
+ | `simulator_create` | Runtime 内创建 Core 模拟器 |
285
+ | `runtime_ready` | Runtime 收到 connect 到返回 ready |
286
+ | `client_ready` | Client 创建到可调用的端到端总耗时 |
287
+ | `package_materialize` | Client 侧 Blob/File 转 ArrayBuffer,带本次加载的 `request_id` |
288
+ | `remote_request` | 所有远程方法调用的端到端耗时,`type` 为方法名;核心加载只筛选 `loadZip` / `loadFiles` |
289
+ | `runtime_request` | Runtime 处理 `loadZip` / `loadFiles` 的耗时,旧 Client 连接时也可采集 |
290
+ | `zip_parse` | 审核 Runtime 的 zip 解压 |
291
+ | `review_wait` | ZIP 解析完成到开始审核,仅隔离人工等待 |
292
+ | `review_app_load` | 开始审核到 Core `loadFiles` 完成 |
293
+ | `renderer_setup` | 渲染器、SW 前置准备和容器初始化 |
294
+ | `injection_build` | 注入脚本与 CSP 构建 |
295
+ | `guest_document` | 小工具 iframe 文档 load |
296
+ | `guest_dom_ready` | 小工具 DOMContentLoaded |
297
+ | `guest_script_execution` | 小工具 DOM 解析及同步脚本执行 |
298
+ | `guest_first_paint` | 小工具首绘 |
299
+ | `guest_resources` | 小工具资源数量和传输体积 |
300
+ | `guest_render` | 小工具 render-done |
301
+ | `guest_error` | 小工具加载阶段错误码与错误信息 |
302
+
303
+ Core 中的 `simulator_parse_done`、`simulator_sw_register`、`simulator_sw_mount`、
304
+ `simulator_load` 和 `simulator_render_done` 使用同一个 `session_id`,核心包加载期间同时带
305
+ `request_id` 和 `type`。核心加载看板依赖这些事件与 `simulator_load_stage`;权限、bridge、
306
+ 销毁等生命周期点位不纳入加载耗时口径。
307
+
308
+ ### 时序
309
+
310
+ ```mermaid
311
+ sequenceDiagram
312
+ participant C as Client
313
+ participant S as Simulator Server
314
+ participant R as Runtime
315
+ participant W as Service Worker
316
+ participant G as Guest iframe
317
+ participant A as APM
318
+
319
+ C->>S: GET runtime-manifest
320
+ C-->>A: runtime_manifest + network phases
321
+ C->>R: navigate Runtime document
322
+ R-->>A: runtime_bootstrap / runtime_script
323
+ C->>R: connect(sessionId, protocol v6)
324
+ R->>S: fetch runtime config
325
+ R-->>A: runtime_config / simulator_create
326
+ R-->>C: ready(protocol v6)
327
+ C-->>A: runtime_handshake / client_ready
328
+ C->>R: loadZip / loadFiles
329
+ C-->>A: package_materialize / remote_request
330
+ R-->>A: runtime_request / zip_parse
331
+ Note over R: review_wait 隔离人工等待
332
+ R->>R: reviewer starts loading
333
+ R->>W: parse files / register / build injection / mount
334
+ W-->>R: MOUNT ACK
335
+ R->>G: navigate virtual entry
336
+ G-->>R: load / DOM ready / first paint / render done
337
+ R-->>A: review_app_load / guest_* stages
338
+ Note over C,A: session_id 串连接,request_id 串单次 loadZip/loadFiles
339
+ ```
@@ -0,0 +1,62 @@
1
+ # 模拟器 Worker 接入
2
+
3
+ 模拟器和客户端使用同一份 `device-capabilities-seal.iife.min.js` 安全脚本文件。
4
+ 脚本在 miniapp-js-api 仓库的 miniapp-secure-runtime 中构建,包含设备封印、Worker 构造函数限制、
5
+ onTerminate/offTerminate 原型 patch、单实例配额和连续隐藏 30 秒回收。
6
+ 模拟器不依赖安全运行时 npm 包,也不导入其源码、函数或类型。
7
+
8
+ 客户端只维护 Worker 的 `enabled` 开关。
9
+ OffscreenCanvas、WebGL 和 transferable 使用原生行为,由业务 JS 检测、实测。
10
+
11
+ ## 本地联调
12
+
13
+ 先构建安全脚本,再将文件显式同步到模拟器:
14
+
15
+ ```bash
16
+ # miniapp-js-api 仓库
17
+ pnpm --filter @skillbuilder/miniapp-secure-runtime build
18
+ pnpm --filter @skillbuilder/miniapp-secure-runtime test:worker
19
+
20
+ # skill-designable-kit 仓库,参数为本次构建产物的绝对路径
21
+ pnpm --filter @skillbuilder/miniapp-simulator sync:secure-runtime -- /absolute/path/to/miniapp-js-api/packages/miniapp-secure-runtime/dist/device-capabilities-seal.iife.min.js
22
+ pnpm --filter @skillbuilder/miniapp-simulator test:worker
23
+ pnpm --filter @skillbuilder/miniapp-simulator build:sw:dev
24
+ pnpm --filter @skillbuilder/miniapp-simulator build:remote:dev
25
+ ```
26
+
27
+ 同步脚本校验文件语法、必要标记和 20,000 字节上限,按原字节复制到
28
+ `src/injection/modules/device-capabilities-seal.iife.min.js` 并输出 SHA-256。
29
+ 该文件随模拟器代码保存;模拟器构建只读取此本地文件,不需要访问另一仓库或安装安全运行时包。
30
+ 安全运行时变更后,显式重新构建、同步文件并验证模拟器。
31
+ 完整客户端配置与约束见 miniapp-secure-runtime 的 `docs/worker-runtime.md`。
32
+
33
+ ## 模拟器职责
34
+
35
+ `src/seal-config.ts` 由模拟器维护设备能力枚举、元数据和白名单配置。
36
+ `src/capabilities/security-head-snippet.ts` 的 `buildSealScriptSource` 负责宿主配置序列化与静态安全脚本拼装。
37
+ `src/worker/adapters/` 由模拟器维护以下内容:
38
+
39
+ - `protocol.ts` / `manifest.json`:宿主接入类型、协议常量和默认 Worker CSP。
40
+ - `csp-policy.ts`:Worker CSP 的校验、逐指令覆盖与默认策略回退。
41
+ - `package-policy.ts`:根据 Worker 开关和会话标识生成宿主配置。
42
+ - `service-worker.ts`:识别 Worker 请求,校验请求归属,将原始文件字节与独立 Worker CSP 返回。
43
+ - `mount-recovery.ts`:资源 Service Worker 重启后,通过可信宿主恢复包快照并校验请求所属会话。
44
+ - `page-lifecycle.ts`:将可信父窗口状态接入私有控制器。
45
+ - `host-lifecycle.ts`:监听 iframe、宿主 CSS 可见性,接入远程 SDK 私有 MessagePort。
46
+ - `scripts.ts`:将安全脚本返回的私有 Worker 控制器连接到模拟器生命周期适配脚本。
47
+
48
+ Apollo `miniappSimulatorWorkerCspPolicies` 的获取、运行配置下发、CSP 策略处理和响应头附加均属于模拟器链路。
49
+ Worker 用户代码原字节加载,不解析、拼接、包装或注入管理片段。
50
+
51
+ ## 配置与安装
52
+
53
+ 宿主通过只读 `__MINITOOL_WORKER_CONTEXT__` 提供 Worker 配置;设备白名单使用
54
+ `__MINI_APP_SEAL_CONFIG__.allow`。`__MINITOOL_WORKER_STATE__` 是运行时只读状态输出。
55
+ 模拟器只注册一次 security 注入,执行完整安全脚本后将私有控制器连接到 iframe/远程宿主生命周期。
56
+ 页面可见性与销毁监听由安全脚本完成。
57
+
58
+ ## 验证
59
+
60
+ 安全运行时维护构造函数、原型 patch、生命周期和产物体积测试;
61
+ 模拟器维护包资源隔离、本地 CSP 策略、原始字节响应及 Chromium 集成测试,覆盖真实 Worker、CSP 阻断、宿主可见性与资源 SW 重启。
62
+ Playground `/worker` 使用远程模拟器和手机壳,包含正常、关闭、旧端未接入三种模式、绘图/传输测试与 ZIP 导出。