@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
package/README.md CHANGED
@@ -1,3 +1,264 @@
1
- # Temporary Holding Version
1
+ # @skillbuilder/miniapp-simulator
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 技术栈无关的小应用 PC 模拟器 SDK,支持 iframe 预览和 Electron webview 审核场景。
4
+ 模拟器默认**无贴图外壳**(`phoneFrame: false`),保留创作发布标准深色圆角设备边框;需要 iPhone / Nexus 贴图壳时显式传 `phoneFrame`。默认开启 `stageFit` 自适应撑满挂载区域;手动拖拽缩放需传 `stageFit: { resizeHandle: true }`。
5
+
6
+ ```bash
7
+ pnpm --filter @skillbuilder/miniapp-simulator run playground
8
+ ```
9
+
10
+ Smoke 联调:`testsuits/miniapp-simulator-sdk-smoke` 以 ESM `import` 引用本包 `lib/`;改源码后 `pnpm --filter @skillbuilder/miniapp-simulator run build:npm`,再 `pnpm --filter miniapp-simulator-sdk-smoke dev` 即可。
11
+
12
+ **设备能力封印**由本包自维护:封印 IIFE 以本地文件形式存放在
13
+ `src/injection/modules/device-capabilities-seal.iife.min.js`,注入时以 `?raw` 引入(更新封印时替换该文件即可)。
14
+
15
+ 兼容性基线:iOS WKWebView (iOS 13+) / Android WebView (Chrome 49+) / 现代 PC 浏览器。
16
+
17
+ **按业务灵活放开能力**(白名单语义):
18
+
19
+ ```ts
20
+ import { createMiniAppSimulator, MiniAppCapability } from "@skillbuilder/miniapp-simulator";
21
+
22
+ createMiniAppSimulator({
23
+ deviceCapabilities: {
24
+ sealAllow: [
25
+ MiniAppCapability.Camera,
26
+ MiniAppCapability.Microphone,
27
+ MiniAppCapability.PhotoLibrary,
28
+ // 后续要放开定位时只追加这一行:
29
+ // MiniAppCapability.Geolocation,
30
+ ],
31
+ },
32
+ });
33
+ ```
34
+
35
+ `sealAllow` 不传 → 等价于 `[Camera, Microphone, PhotoLibrary]`;`sealAllow: []` → 严格模式全封禁。Android / iOS 注入路径见 `docs/local-dev.md`。
36
+
37
+ **支持的能力已枚举化对外**,可直接用于配置面板 / 校验(由本包自维护,`src/seal-config.ts`):
38
+
39
+ ```ts
40
+ import {
41
+ MINI_APP_CAPABILITIES, // 全量能力元数据(value / controlledBy / description / permissionNames)
42
+ SEAL_CONTROLLED_CAPABILITIES, // 受 sealAllow 白名单实际控制的能力
43
+ isMiniAppCapability,
44
+ sanitizeCapabilityList,
45
+ } from "@skillbuilder/miniapp-simulator";
46
+ ```
47
+
48
+ ## Worker Guard
49
+
50
+ 设备封印与 Worker Guard 合并在同一份安全 IIFE 中,只在页面业务代码之前执行。**用户 `worker.js` 必须原字节加载,
51
+ 不解析、不拼接、不包装、不注入代码**;Worker 内保持原生 API,限制由入口响应 CSP 执行。
52
+
53
+ ```ts
54
+ const simulator = createMiniAppSimulator({
55
+ mount: "#preview",
56
+ serviceWorker: { scriptUrl: "/miniapp-simulator/service-worker.js" },
57
+ security: { worker: { enabled: true } },
58
+ });
59
+ // 业务按原生 URL 规则加载脚本,例如 new Worker("./tasks/compute.js")。
60
+ ```
61
+
62
+ 默认关闭 Worker,`loadZip` / `loadFiles` 通过 `enabled` 开关启用 classic Worker。
63
+ 脚本地址按原生 URL 规则解析,资源 SW 为当前包中的 Worker 文件返回原始字节与 CSP。
64
+ 页面 CSP 仅允许同源 Worker 资源;module、SharedWorker 与业务 Service Worker 禁用。
65
+ `loadApp` 远程加载和 Electron webview 使用关闭配置。
66
+
67
+ 安全脚本在 miniapp-js-api 仓库的 miniapp-secure-runtime 中构建。
68
+ 模拟器只消费构建好的 `device-capabilities-seal.iife.min.js` 文件,不依赖该 npm 包或其源码、函数、类型。
69
+ 模拟器自己维护能力枚举、配置、CSP 策略及 `src/worker/adapters/` 中的资源与宿主生命周期接线。
70
+ 客户端与模拟器使用相同脚本字节,通过 `sync:secure-runtime` 显式同步到本地注入文件。
71
+ 构建上限为 20,000 字节(UTF-8,未 gzip),超限失败。无解析器、外部 import 或运行时加载。
72
+
73
+ 宿主在安全脚本执行前注入只读 `__MINITOOL_WORKER_CONTEXT__`,配置包含 `protocolVersion: 1`、
74
+ 独立执行 `sessionId`、`enabled` 与可选初始可见状态 `initialState`。
75
+ 页面每次执行最多一个原生 Worker,支持原生消息、继承和数据转移;页面销毁后永久关闭。
76
+ 连续隐藏 30 秒后回收;未满 30 秒恢复可见取消计时。`Worker.prototype.onTerminate/offTerminate`
77
+ 订阅终止原因:业务主动为 `主动`,平台回收为 `system`;恢复后由业务自行重建。
78
+ 宿主用只读 `__MINITOOL_WORKER_CONTEXT__` 配置 Worker;`__MINITOOL_WORKER_STATE__` 和启动参数输出实时状态。
79
+ 客户端只维护 Worker 总开关,OffscreenCanvas、WebGL、ArrayBuffer 转移与 ImageBitmap 由业务 JS 检测原生支持情况。
80
+ 显式 terminate 释放名额,普通 error 不释放。`self.close()` 无法从页面可靠感知,名额保守保留到
81
+ 父页面显式 terminate 或会话结束,不增加任何管理消息。
82
+
83
+ 资源层校验文件与页面归属,原样返回文件字节并设置独立 CSP、MIME 与 nosniff;失败直接 403,
84
+ 不回源。`connect-src 'none'` 限制网络,`script-src 'none'` 限制追加脚本与动态求值,
85
+ `worker-src 'none'` 与 `child-src 'none'` 限制子 Worker。客户端需在资源加载层下发相同 Worker CSP。
86
+
87
+ 公共运行时协议为 1,模拟器资源 Service Worker 协议为 4,Runtime 与资源 SW 随同一 release 发布。
88
+ JS 配额属于当前 Realm 的软限制,其他 Realm、硬 CPU/内存限制和 Worker 内额外平台 API 由容器控制。
89
+ 具体多端接入与限制见 [Worker Guard 接入说明](docs/worker-runtime.md)。
90
+ Worker CSP 支持独立 Apollo Key `miniappSimulatorWorkerCspPolicies`,经 runtime-config 的
91
+ `workerCsp` 字段下发;本地配置为 `security.workerCsp`。配置缺失或非法时采用内置策略,
92
+ 仅对新建连接及挂载生效,不改变用户文件内容。
93
+
94
+ ```bash
95
+ pnpm --filter @skillbuilder/miniapp-simulator test:worker
96
+ # 使用已有 Chromium:WORKER_SEAL_CHROMIUM_EXECUTABLE=/absolute/path/to/chrome pnpm --filter @skillbuilder/miniapp-simulator test:worker
97
+ ```
98
+
99
+ ## 快速接入
100
+
101
+ ```ts
102
+ import { createMiniAppSimulator } from "@skillbuilder/miniapp-simulator";
103
+
104
+ // 1) 创建实例(mount 为 CSS 选择器或 HTMLElement;create 只管配置,不传入口)
105
+ const simulator = createMiniAppSimulator({
106
+ mount: "#preview",
107
+ viewport: { width: 390, height: 844 },
108
+ });
109
+
110
+ // 2) 加载入口(三选一):远程 URL / .zip 包 / 目录文件表
111
+ await simulator.loadApp("https://cdn.../index.html", { query: { versionId: "v2" } });
112
+ ```
113
+
114
+ ## DOM / Canvas 场景对象 Visual Inspector
115
+
116
+ Visual Inspector 统一输出 DOM 与 PixiJS、Three.js、自定义 Canvas 场景对象。DOM 继续兼容
117
+ `elementSelected`;新消费方应订阅 `visualSelected` / `visualSelectionChange`,并通过
118
+ `getSceneTree()` 与 `selectByRef()` 实现 Viewport、Hierarchy、Inspector 双向联动:
119
+
120
+ ```ts
121
+ simulator.on("visualSelected", (selection) => {
122
+ if (selection.kind === "scene") {
123
+ console.log(selection.engine, selection.nodePath, selection.properties, selection.source);
124
+ }
125
+ });
126
+
127
+ const tree = await simulator.inspector.getSceneTree();
128
+ const firstSceneNode = tree.providers[0]?.roots[0]?.children[0];
129
+ if (firstSceneNode) await simulator.inspector.selectByRef(firstSceneNode.ref);
130
+ ```
131
+
132
+ 场景 Provider 由小应用模板主动注册;Provider 缺失或失败时仍会降级为普通 canvas DOM
133
+ 选择。普通预览默认不代理页面调度器;明确的 2D / 3D 游戏由上游传入
134
+ `visualInspector: { pauseRuntime: true }`。此时进入选取模式会冻结 RAF、timer、CSS 动画与媒体,
135
+ 并调用 Provider 可选的 `pause()`;退出时按原状态恢复,因此运动对象可以稳定命中。
136
+ 完整职责、协议和分期见
137
+ [`design/mini-app/mini-app-visual-inspector-design.md`](./design/mini-app/mini-app-visual-inspector-design.md)。
138
+
139
+ 独立验证 2D / 3D 场景圈选:
140
+
141
+ ```bash
142
+ pnpm --filter @skillbuilder/miniapp-simulator demo:inspector
143
+ ```
144
+
145
+ 打开 `http://127.0.0.1:5181`,可手动点击运动元素,也可运行层级读取和反向选择 Case。
146
+
147
+ 独立验证 AI 对话 Bridge(非流式完整消息与流式增量渲染):
148
+
149
+ ```bash
150
+ pnpm --filter @skillbuilder/miniapp-simulator demo:ai
151
+ ```
152
+
153
+ 打开 `http://127.0.0.1:5182`,两个 Case 会自动运行并展示业务层最终渲染结果。
154
+
155
+ ## 加载 .zip 应用包
156
+
157
+ 实例方法 `loadZip` 自动完成「解压 → 解析 `manifest.json` → 内联相对资源 → 建 blob URL 加载」,
158
+ 入参支持 `Blob | File | ArrayBuffer | Uint8Array`:
159
+
160
+ ```ts
161
+ import { createMiniAppSimulator } from "@skillbuilder/miniapp-simulator";
162
+ import JSZip from "jszip"; // 或在全局挂载 window.JSZip(CDN / UMD)
163
+
164
+ const simulator = createMiniAppSimulator({ mount: "#preview" });
165
+ await simulator.load();
166
+
167
+ // 拖拽 / 选择得到的 .zip File 直接喂入;返回包元信息(文件清单 + manifest 解析结果)
168
+ const result = await simulator.loadZip(zipFile, { jszip: JSZip });
169
+ console.log(result.files, result.manifest.window, result.manifest.warnings);
170
+ ```
171
+
172
+ > 本包**技术栈无关**、不强依赖 JSZip:通过 `loadZip(source, { jszip })` 传入 JSZip 兼容对象,
173
+ > 或在全局挂载 `window.JSZip`(不传时自动回退);二者皆缺失会抛出 `ZIP_LOAD_FAILED`。
174
+
175
+ 更多说明见:
176
+
177
+ - [`docs/remote-architecture.md`](./docs/remote-architecture.md):远程模式架构、版本发布、动态更新与隔离边界
178
+ - [`docs/remote-usage.md`](./docs/remote-usage.md):远程模式接入、实例 API、Host API 与多实例用法
179
+ - [`docs/server-playground-https-debugging.md`](./docs/server-playground-https-debugging.md):Server Playground 本地 HTTPS、证书信任与 Creator AI 独立泳道联调
180
+ - [`docs/external-cli-workbench.md`](./docs/external-cli-workbench.md):外部 CLI 的 `xxx dev` 参数与本地托管契约
181
+ - `docs/sdk-usage.md`
182
+ - `docs/local-dev.md`
183
+ - `docs/electron-review.md`
184
+
185
+ ## 外部 AI 调试工作台
186
+
187
+ `@skillbuilder/miniapp-simulator/workbench` 在现有 Simulator 外增加一层轻量工作台壳,面向
188
+ Codex、CodeBuddy 和本地 CLI 展示运行状态、诊断事件、重新加载与截图入口。它不会改变 Simulator
189
+ 的加载、渲染或调试协议:调用方仍可通过 `workbench.simulator` 使用完整 SDK 能力。
190
+
191
+ ```ts
192
+ import { createMiniAppWorkbench } from "@skillbuilder/miniapp-simulator/workbench";
193
+ import { BusinessLine } from "@skillbuilder/miniapp-simulator/remote";
194
+
195
+ const workbench = createMiniAppWorkbench({
196
+ mount: "#app",
197
+ projectName: "demo-minitool",
198
+ viewport: { width: 393, height: 852 },
199
+ remote: {
200
+ serviceUrl: "https://miniapp-sandbox.xiaohongshu.com/simulator/",
201
+ appId: "demo-minitool",
202
+ businessLine: BusinessLine.skillDesignable,
203
+ },
204
+ });
205
+
206
+ // 本地 Agent 调试使用目录文件表,不需要 ZIP。
207
+ await workbench.loadFiles({
208
+ "index.html": htmlText,
209
+ "main.js": scriptText,
210
+ "logo.png": pngBytes,
211
+ }, { entry: "index.html" });
212
+ ```
213
+
214
+ 工作台使用宿主侧薄 SDK 连接 Simulator Server:薄 SDK 通过 `runtime-manifest` 动态选择远端 Runtime,
215
+ Runtime 再注册同源、版本化的 Service Worker。CLI 不复制或托管 Runtime / SW,也不直接执行小工具代码。
216
+
217
+ 包内同时提供 `@skillbuilder/miniapp-simulator/workbench.html` 静态入口,供 CLI 等本地服务直接托管;
218
+ 页面结构、样式和 Simulator 编排均由本包维护。
219
+
220
+ CLI 可直接调用独立的 Node 服务入口,传入调试根目录即可启动完整工作台:
221
+
222
+ ```ts
223
+ import { startWorkbenchServer } from "@skillbuilder/miniapp-simulator/workbench/server";
224
+
225
+ const server = await startWorkbenchServer({ rootDir: "./dist" });
226
+ console.log(server.url);
227
+ // CLI 退出时调用 await server.close()。
228
+ ```
229
+
230
+ Node 入口统一负责 HTML 入口识别、文件清单与原始资源托管,工作台通过 `loadFiles()` 加载目录文件,
231
+ 继续经 Remote SDK 连接远端 Runtime / SW。根目录及子目录中的 HTML、JS、CSS 变化会自动重载小工具,
232
+ 无需重启服务或手动刷新;“重新运行”仍可手动触发。连续写入会合并,重载期间的新变更会串行处理。
233
+
234
+ 工作台固定使用 SDK 的 `rednote` 壳层,保留状态栏、返回等原生控件。支持全屏小工具、半屏小工具、笔记页、个人页四种场景,设备尺寸独立切换。
235
+ 可通过工作台控件或 `workbench.setScene("half")` 切换;文件更新后保持当前选定场景。
236
+
237
+ 仓库内可用 Mock 脚本模拟另一套 CLI 的调用:
238
+
239
+ ```shell
240
+ pnpm --filter @skillbuilder/miniapp-simulator mock:cli-dev -- \
241
+ --dir /absolute/path/to/minitool/dist
242
+ ```
243
+
244
+ 外部 CLI 只需直接安装本包:`--dir` 对应 Node 入口的 `rootDir`,`--no-open` 由 CLI 自己控制打开浏览器,`-h` / `--help` 输出帮助。
245
+ 设备、场景、入口识别和文件监听由工作台处理;完整参数与职责见 [`external-cli-workbench.md`](./docs/external-cli-workbench.md)。
246
+
247
+ `prepack` 自动执行本地构建;不执行上传或发布。Runtime / SW 仍从远程服务动态加载,外网可达性需单独验收。
248
+ 当前验证范围及外发门禁见 [`npm-integration-verification.md`](./docs/npm-integration-verification.md)。
249
+
250
+ ### 仅发布模拟器 npm(仓库根目录执行)
251
+
252
+ ```shell
253
+ pnpm release:simulator snapshot --registry public --dry-run # 构建、打包、检查,不发布
254
+ pnpm release:simulator snapshot --registry public # beta 快照,仅发布本包
255
+ pnpm release:simulator stable --registry public # 当前版本 → latest
256
+ pnpm release:simulator snapshot --registry internal # 同样支持内部源
257
+ ```
258
+
259
+ 默认源为 `public`,也支持 `--registry <url>`。真实发布需最终确认,CI 显式传 `--yes`。
260
+ 正式版沿用本包版本,且只允许在 `release/release_*` 分支发布;快照版本仅写入临时目录,不修改仓库版本、lockfile 或 changeset。
261
+ 构建依赖 `endpoints` 不发布;脚本不执行全仓构建或 CDN 上传,外部运行依赖出现内部包时会阻断发布。
262
+ 真实发布成功后向现有两个机器人发送版本与安装命令;dry-run 不发送通知,通知失败只告警、不重试发布。
263
+ 发布归档会移除开发依赖、构建/发布脚本与源覆盖配置;dry-run 产物及清单保留在打印的临时目录中。
264
+ 该脚本不替代包内文档/资源的外发审查、npm scope 发布权限或远程 Runtime / SW 外网可达性验收。
Binary file
@@ -0,0 +1,77 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>小红书小工具 · 本地调试工作台</title>
7
+ <style>
8
+ html,
9
+ body,
10
+ #workbench {
11
+ width: 100%;
12
+ height: 100%;
13
+ margin: 0;
14
+ overflow: hidden;
15
+ }
16
+ </style>
17
+ </head>
18
+ <body>
19
+ <div id="workbench"></div>
20
+ <script type="module">
21
+ async function fetchResource(url, init) {
22
+ const response = await fetch(url, { cache: "no-store", ...init });
23
+ if (!response.ok) throw new Error(`${url} 加载失败:HTTP ${response.status}`);
24
+ return response;
25
+ }
26
+
27
+ async function start() {
28
+ const config = await fetchResource("/__miniapp_workbench__/config.json").then((response) =>
29
+ response.json(),
30
+ );
31
+ if (config.artifact.type !== "files") {
32
+ throw new Error(`不支持的产物类型:${config.artifact.type}`);
33
+ }
34
+ const loadLocalFiles = async () => {
35
+ const { entry, files } = await fetchResource(config.artifact.url).then((response) =>
36
+ response.json(),
37
+ );
38
+ const entries = await Promise.all(
39
+ files.map(async (file) => {
40
+ const url =
41
+ config.artifact.baseUrl + file.split("/").map(encodeURIComponent).join("/");
42
+ const content = await fetchResource(url).then((response) => response.arrayBuffer());
43
+ return [file, content];
44
+ }),
45
+ );
46
+ await workbench.loadFiles(Object.fromEntries(entries), { entry });
47
+ };
48
+ const { createMiniAppWorkbench } = await import(config.sdkUrl);
49
+ const workbench = createMiniAppWorkbench({
50
+ mount: "#workbench",
51
+ projectName: config.projectName,
52
+ viewport: config.viewport,
53
+ remote: config.remote,
54
+ onReload: loadLocalFiles,
55
+ });
56
+ window.__MINIAPP_WORKBENCH__ = workbench;
57
+
58
+ const changes = new EventSource(config.artifact.eventsUrl);
59
+ changes.addEventListener("reload", () => void workbench.reload());
60
+ changes.addEventListener("watch-error", (event) =>
61
+ console.error("[miniapp-workbench]", event.data),
62
+ );
63
+ window.addEventListener("beforeunload", () => changes.close(), { once: true });
64
+ }
65
+
66
+ start().catch((error) => {
67
+ console.error("[miniapp-workbench]", error);
68
+ if (window.__MINIAPP_WORKBENCH__) return;
69
+ const mount = document.querySelector("#workbench");
70
+ if (!mount) return;
71
+ mount.style.cssText =
72
+ "display:grid;place-items:center;padding:24px;box-sizing:border-box;background:#f5f6f8;color:#34353a;font:13px/1.6 -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif";
73
+ mount.textContent = `工作台启动失败:${error instanceof Error ? error.message : String(error)}`;
74
+ });
75
+ </script>
76
+ </body>
77
+ </html>