@skillbuilder/miniapp-simulator 0.0.0-stage → 0.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.
Files changed (229) 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 +404 -0
  28. package/docs/remote-load-observability.md +268 -0
  29. package/docs/remote-usage.md +371 -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/business-line-CMb3ehRn.mjs +15 -0
  37. package/lib/client-CO-UjIY3.mjs +775 -0
  38. package/lib/index-B5oixYXr.mjs +574 -0
  39. package/lib/index.js +10763 -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/rednote-preview/chrome-tone.d.ts +44 -0
  123. package/lib/types/native-ui/rednote-preview/defaults.d.ts +30 -0
  124. package/lib/types/native-ui/rednote-preview/half-sheet-drag.d.ts +61 -0
  125. package/lib/types/native-ui/rednote-preview/index.d.ts +2 -0
  126. package/lib/types/native-ui/rednote-preview/register.d.ts +7 -0
  127. package/lib/types/native-ui/rednote-preview/styles.d.ts +4 -0
  128. package/lib/types/native-ui/rednote-preview/xhs-note-detail-shell.d.ts +70 -0
  129. package/lib/types/native-ui/rednote-preview/xhs-note-half-sheet.d.ts +100 -0
  130. package/lib/types/native-ui/rednote-preview/xhs-profile-shell.d.ts +31 -0
  131. package/lib/types/native-ui/toast/frame-toast-host.d.ts +18 -0
  132. package/lib/types/native-ui/toast/index.d.ts +19 -0
  133. package/lib/types/presets.d.ts +11 -0
  134. package/lib/types/remote/client.d.ts +89 -0
  135. package/lib/types/remote/jsapi-config.d.ts +9 -0
  136. package/lib/types/remote/load-timing.d.ts +46 -0
  137. package/lib/types/remote/protocol.d.ts +165 -0
  138. package/lib/types/remote/review-launch.d.ts +6 -0
  139. package/lib/types/remote/runtime-entry.d.ts +1 -0
  140. package/lib/types/remote/runtime.d.ts +14 -0
  141. package/lib/types/remote/simulator-prompt.d.ts +9 -0
  142. package/lib/types/remote/widget-snapshot-guest-runtime.d.ts +1 -0
  143. package/lib/types/remote-client.d.ts +5 -0
  144. package/lib/types/renderer/base.d.ts +8 -0
  145. package/lib/types/renderer/content-host/iframe-content-host.d.ts +84 -0
  146. package/lib/types/renderer/content-host/index.d.ts +3 -0
  147. package/lib/types/renderer/content-host/types.d.ts +89 -0
  148. package/lib/types/renderer/content-host/webview-content-host.d.ts +53 -0
  149. package/lib/types/renderer/iframe.d.ts +93 -0
  150. package/lib/types/renderer/index.d.ts +2 -0
  151. package/lib/types/renderer/rednote-shell.d.ts +73 -0
  152. package/lib/types/renderer/webview.d.ts +59 -0
  153. package/lib/types/runtime/context.d.ts +31 -0
  154. package/lib/types/runtime/jsapi-modules/context.d.ts +19 -0
  155. package/lib/types/runtime/jsapi-modules/filesystem.d.ts +19 -0
  156. package/lib/types/runtime/jsapi-modules/index.d.ts +9 -0
  157. package/lib/types/runtime/jsapi-modules/interaction.d.ts +4 -0
  158. package/lib/types/runtime/jsapi-modules/location.d.ts +3 -0
  159. package/lib/types/runtime/jsapi-modules/media.d.ts +13 -0
  160. package/lib/types/runtime/jsapi-modules/network.d.ts +3 -0
  161. package/lib/types/runtime/jsapi-modules/storage.d.ts +26 -0
  162. package/lib/types/runtime/jsapi-modules/system.d.ts +4 -0
  163. package/lib/types/runtime/jsapi-modules/types.d.ts +21 -0
  164. package/lib/types/runtime/jsapi-modules/window-info.d.ts +43 -0
  165. package/lib/types/runtime/request/host-request.d.ts +13 -0
  166. package/lib/types/runtime/request/request-manager-frame.d.ts +29 -0
  167. package/lib/types/runtime/request/request-runtime.d.ts +26 -0
  168. package/lib/types/runtime/request/request-types.d.ts +26 -0
  169. package/lib/types/runtime/request/sse-text-framer.d.ts +16 -0
  170. package/lib/types/seal-config.d.ts +134 -0
  171. package/lib/types/sw/index.d.ts +9 -0
  172. package/lib/types/sw/parse-raw.d.ts +34 -0
  173. package/lib/types/sw/protocol.d.ts +19 -0
  174. package/lib/types/sw/served-entry.d.ts +9 -0
  175. package/lib/types/sw/service-worker-content-host.d.ts +11 -0
  176. package/lib/types/sw/service-worker-host.d.ts +74 -0
  177. package/lib/types/sw/service-worker-renderer.d.ts +10 -0
  178. package/lib/types/sw/worker/fs-codec.d.ts +23 -0
  179. package/lib/types/sw/worker/fs-ops.d.ts +22 -0
  180. package/lib/types/sw/worker/fs-store.d.ts +29 -0
  181. package/lib/types/sw/worker/globals.d.ts +50 -0
  182. package/lib/types/sw/worker/main.d.ts +6 -0
  183. package/lib/types/sw/worker/mime.d.ts +2 -0
  184. package/lib/types/sw/worker/mount-store.d.ts +48 -0
  185. package/lib/types/sw/worker/paths.d.ts +4 -0
  186. package/lib/types/sw/worker/serve.d.ts +13 -0
  187. package/lib/types/tracker/constants.d.ts +14 -0
  188. package/lib/types/tracker/default-tracker.d.ts +19 -0
  189. package/lib/types/tracker/index.d.ts +70 -0
  190. package/lib/types/transport/bridge-protocol.d.ts +78 -0
  191. package/lib/types/transport/iframe-transport.d.ts +39 -0
  192. package/lib/types/transport/index.d.ts +14 -0
  193. package/lib/types/transport/jsapi-bridge-messages.d.ts +36 -0
  194. package/lib/types/transport/types.d.ts +48 -0
  195. package/lib/types/transport/webview-transport.d.ts +52 -0
  196. package/lib/types/types/events.d.ts +130 -0
  197. package/lib/types/types/index.d.ts +11 -0
  198. package/lib/types/types/inspector.d.ts +172 -0
  199. package/lib/types/types/jsapi.d.ts +18 -0
  200. package/lib/types/types/options.d.ts +851 -0
  201. package/lib/types/types/renderer.d.ts +145 -0
  202. package/lib/types/utils/ancestor-scroll.d.ts +11 -0
  203. package/lib/types/utils/error-svg.d.ts +12 -0
  204. package/lib/types/utils/frameless-shell.d.ts +114 -0
  205. package/lib/types/utils/miniapp-chrome-svg.d.ts +47 -0
  206. package/lib/types/utils/package-constants.d.ts +8 -0
  207. package/lib/types/utils/screenshot.d.ts +14 -0
  208. package/lib/types/utils/simulator-pc-guard.d.ts +10 -0
  209. package/lib/types/utils/stage-fit.d.ts +6 -0
  210. package/lib/types/utils/touch-emulate.d.ts +26 -0
  211. package/lib/types/utils/zip-entry.d.ts +13 -0
  212. package/lib/types/workbench/index.d.ts +63 -0
  213. package/lib/types/workbench/preview-transition.d.ts +8 -0
  214. package/lib/types/workbench/server/artifact.d.ts +8 -0
  215. package/lib/types/workbench/server/index.d.ts +19 -0
  216. package/lib/types/workbench/server/watch.d.ts +6 -0
  217. package/lib/types/workbench/view.d.ts +36 -0
  218. package/lib/types/worker/adapters/csp-policy.d.ts +7 -0
  219. package/lib/types/worker/adapters/host-lifecycle.d.ts +5 -0
  220. package/lib/types/worker/adapters/mount-recovery.d.ts +2 -0
  221. package/lib/types/worker/adapters/package-policy.d.ts +12 -0
  222. package/lib/types/worker/adapters/page-lifecycle.d.ts +1 -0
  223. package/lib/types/worker/adapters/protocol.d.ts +49 -0
  224. package/lib/types/worker/adapters/scripts.d.ts +2 -0
  225. package/lib/types/worker/adapters/service-worker.d.ts +2 -0
  226. package/lib/workbench-server.mjs +183 -0
  227. package/lib/workbench.js +1404 -0
  228. package/package.json +61 -4
  229. package/scripts/mock-cli-dev.mjs +52 -0
@@ -0,0 +1,12 @@
1
+ import type { WorkerCspPolicy } from "./csp-policy";
2
+ import type { WorkerGuardConfig } from "./protocol";
3
+ export interface MiniAppWorkerOptions {
4
+ /** 仅 loadZip/loadFiles 的包内 classic Worker 可启用,默认 false。 */
5
+ enabled?: boolean;
6
+ }
7
+ /** 随 MOUNT 发送给资源 Service Worker 的会话策略。 */
8
+ export interface WorkerMountPolicy {
9
+ sessionId: string;
10
+ csp?: WorkerCspPolicy;
11
+ }
12
+ export declare function createPackageWorkerConfig(options: MiniAppWorkerOptions | undefined, sessionId: string): WorkerGuardConfig | undefined;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ /** 宿主在业务代码前提供本次页面会话的 Worker 配置。 */
2
+ export interface WorkerGuardConfig {
3
+ protocolVersion: number;
4
+ sessionId: string;
5
+ enabled: boolean;
6
+ /** 宿主初始可见性;默认 visible,与 document.hidden 共同判断。 */
7
+ initialState?: "visible" | "hidden";
8
+ }
9
+ export interface WorkerRuntimeMetadata {
10
+ readonly name: string;
11
+ readonly version: string;
12
+ readonly protocolVersion: number;
13
+ readonly workerCsp: string;
14
+ }
15
+ /** 模拟器维护的安全脚本接入协议;与资源 Service Worker 协议独立。 */
16
+ export declare const WORKER_RUNTIME_METADATA: WorkerRuntimeMetadata;
17
+ export declare const WORKER_RUNTIME_PROTOCOL_VERSION: number;
18
+ export declare const WORKER_CONTENT_SECURITY_POLICY: string;
19
+ export declare const WORKER_BACKGROUND_TIMEOUT_MS = 30000;
20
+ export type WorkerTerminateFlag = "主动" | "system";
21
+ export type WorkerTerminateCallback = (flag: WorkerTerminateFlag) => void;
22
+ export interface WorkerRuntimeState {
23
+ readonly protocolVersion: number;
24
+ readonly sessionId: string;
25
+ readonly enabled: boolean;
26
+ readonly maxWorkers: number;
27
+ readonly state: "visible" | "hidden" | "destroyed";
28
+ readonly recycled: boolean;
29
+ }
30
+ export interface WorkerGuardController {
31
+ readonly supported: boolean;
32
+ readonly context: WorkerRuntimeState;
33
+ updateVisibility(visible: boolean): void;
34
+ reclaim(): void;
35
+ destroy(): void;
36
+ }
37
+ declare global {
38
+ interface Worker {
39
+ /** 平台终止通知;浏览器/系统直接结束进程及 self.close 不保证可观测。 */
40
+ onTerminate(callback: WorkerTerminateCallback): void;
41
+ offTerminate(callback?: WorkerTerminateCallback): void;
42
+ }
43
+ interface Window {
44
+ /** 宿主在安全脚本前注入的 Worker 配置,只读、同一页面会话固定。 */
45
+ readonly __MINITOOL_WORKER_CONTEXT__?: Readonly<WorkerGuardConfig>;
46
+ /** 运行时输出;业务只读,不能作为控制入口。 */
47
+ readonly __MINITOOL_WORKER_STATE__: WorkerRuntimeState;
48
+ }
49
+ }
@@ -0,0 +1,2 @@
1
+ /** 在私有闭包中连接安全脚本返回的 Worker 控制器与模拟器宿主消息通道。 */
2
+ export declare function buildWorkerLifecycleScript(sealSource: string): string;
@@ -0,0 +1,2 @@
1
+ /** 为当前包页面的 Worker 请求返回原始文件与 CSP,保持实例间资源隔离。 */
2
+ export declare function serveWorkerRequest(request: Request, clientId: string): Promise<Response>;
@@ -0,0 +1,183 @@
1
+ import { once as I } from "node:events";
2
+ import u from "node:fs/promises";
3
+ import { createRequire as A } from "node:module";
4
+ import n from "node:path";
5
+ import { format as U } from "node:url";
6
+ import j from "@koa/router";
7
+ import { send as S } from "@koa/send";
8
+ import F from "koa";
9
+ import { PassThrough as _ } from "node:stream";
10
+ import { watch as L } from "chokidar";
11
+ const O = "https://miniapp-sandbox.xiaohongshu.com/simulator/";
12
+ var b = /* @__PURE__ */ ((e) => (e.skillDesignable = "skill-designable", e.creator = "creator", e.mars = "mars", e.dev = "dev", e))(b || {});
13
+ function C(e, t) {
14
+ const s = e.filter(
15
+ (r) => !r.includes("/") && n.extname(r).toLowerCase() === ".html"
16
+ );
17
+ if (s.includes("index.html")) return "index.html";
18
+ if (s.length === 1) return s[0];
19
+ throw new Error(
20
+ s.length === 0 ? `调试根目录中没有 HTML 文件:${t}` : `调试根目录中有多个 HTML 文件,请将入口命名为 index.html:${t}`
21
+ );
22
+ }
23
+ async function g(e) {
24
+ const t = [];
25
+ async function s(r) {
26
+ for (const i of await u.readdir(r, { withFileTypes: !0 })) {
27
+ if (i.name.startsWith(".")) continue;
28
+ const l = n.join(r, i.name);
29
+ i.isDirectory() ? await s(l) : i.isFile() && t.push(n.relative(e, l).split(n.sep).join("/"));
30
+ }
31
+ }
32
+ return await s(e), { entry: C(t, e), files: t };
33
+ }
34
+ async function N(e) {
35
+ const t = n.resolve(e);
36
+ if (!(await u.stat(t).catch(() => null))?.isDirectory()) throw new Error(`调试根目录不存在:${t}`);
37
+ return u.realpath(t);
38
+ }
39
+ async function D(e, t) {
40
+ const s = await u.realpath(n.resolve(e, t)), r = n.relative(e, s);
41
+ if (r === ".." || r.startsWith(`..${n.sep}`) || n.isAbsolute(r))
42
+ throw Object.assign(new Error("Forbidden"), { status: 403 });
43
+ return s;
44
+ }
45
+ const E = `event: reload
46
+ data: {}
47
+
48
+ `, H = /\.(?:html|[cm]?js|css)$/i;
49
+ async function M(e) {
50
+ const t = /* @__PURE__ */ new Set();
51
+ let s, r = !1;
52
+ const i = (a) => {
53
+ for (const h of t) h.write(a);
54
+ }, l = L(e, {
55
+ ignoreInitial: !0,
56
+ followSymlinks: !1,
57
+ atomic: !0,
58
+ awaitWriteFinish: { stabilityThreshold: 150, pollInterval: 50 },
59
+ ignored: (a, h) => h?.isSymbolicLink() || n.relative(e, a).split(n.sep).some((f) => f.startsWith(".") || f === "node_modules")
60
+ });
61
+ l.on("all", (a, h) => {
62
+ r || !["add", "change", "unlink"].includes(a) || !H.test(h) || (clearTimeout(s), s = setTimeout(() => i(E), 200));
63
+ }), l.on("error", () => {
64
+ i(`event: watch-error
65
+ data: 本地文件监听异常,请检查目录或重启本地服务。
66
+
67
+ `);
68
+ });
69
+ try {
70
+ await I(l, "ready");
71
+ } catch (a) {
72
+ throw r = !0, clearTimeout(s), await l.close(), a;
73
+ }
74
+ const d = setInterval(() => i(`: heartbeat
75
+
76
+ `), 15e3);
77
+ return d.unref(), {
78
+ subscribe() {
79
+ if (r) throw Object.assign(new Error("Service unavailable"), { status: 503 });
80
+ const a = new _();
81
+ return t.add(a), a.once("close", () => t.delete(a)), a.write(`retry: 1000
82
+ ${E}`), a;
83
+ },
84
+ async close() {
85
+ r = !0, clearTimeout(s), clearInterval(d);
86
+ for (const a of t) a.end();
87
+ t.clear(), await l.close();
88
+ }
89
+ };
90
+ }
91
+ const k = "127.0.0.1", c = "/__miniapp_workbench__", P = async (e, t) => {
92
+ if (e.set({ "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" }), e.method !== "GET" && e.method !== "HEAD") {
93
+ e.set("Allow", "GET, HEAD"), e.status = 405, e.body = "Method Not Allowed";
94
+ return;
95
+ }
96
+ try {
97
+ decodeURI(e.path), await t();
98
+ } catch (s) {
99
+ const r = s, i = ["ENOENT", "ENOTDIR", "EISDIR"].includes(r.code ?? "");
100
+ e.status = r.status ?? (i ? 404 : 500), s instanceof URIError && (e.status = 400), e.type = "text", e.body = "Resource unavailable";
101
+ }
102
+ };
103
+ async function R(e, t, s) {
104
+ const r = await D(t, s), i = n.relative(t, r);
105
+ e.assert(i, 404), await S(e, encodeURIComponent(i), {
106
+ root: t,
107
+ gzip: !1,
108
+ brotli: !1
109
+ });
110
+ }
111
+ async function Z(e) {
112
+ const t = new URL(e.remote?.serviceUrl ?? O);
113
+ if (!["http:", "https:"].includes(t.protocol))
114
+ throw new Error("remote.serviceUrl 仅支持 http / https URL");
115
+ const s = e.remote?.businessLine ?? b.skillDesignable;
116
+ if (!Object.values(b).includes(s))
117
+ throw new Error(`不支持的 businessLine:${s}`);
118
+ const r = await N(e.rootDir);
119
+ await g(r);
120
+ const i = A(import.meta.url).resolve(
121
+ "@skillbuilder/miniapp-simulator/workbench.html"
122
+ ), l = n.resolve(n.dirname(i), "../.."), d = await u.realpath(n.join(l, "lib"));
123
+ try {
124
+ await u.access(n.join(d, "workbench.js"));
125
+ } catch {
126
+ throw new Error("工作台 SDK 产物缺失,请先执行 build:npm");
127
+ }
128
+ const a = {
129
+ schemaVersion: 2,
130
+ projectName: n.basename(n.basename(r) === "dist" ? n.dirname(r) : r),
131
+ viewport: { width: 393, height: 852 },
132
+ sdkUrl: `${c}/sdk/workbench.js`,
133
+ remote: {
134
+ serviceUrl: t.href,
135
+ appId: e.remote?.appId?.trim() || "local-workbench",
136
+ businessLine: s,
137
+ tracker: !1
138
+ },
139
+ artifact: {
140
+ type: "files",
141
+ url: `${c}/manifest.json`,
142
+ baseUrl: `${c}/files/`,
143
+ eventsUrl: `${c}/events`
144
+ }
145
+ }, h = await u.readFile(i), f = await M(r), p = new F();
146
+ p.silent = !0;
147
+ const m = new j();
148
+ p.use(P), m.get(["/", "/index.html"], (o) => {
149
+ o.type = "html", o.body = h;
150
+ }), m.get(`${c}/config.json`, (o) => {
151
+ o.body = a;
152
+ }), m.get(`${c}/manifest.json`, async (o) => {
153
+ o.body = await g(r);
154
+ }), m.get(`${c}/files/*file`, (o) => R(o, r, o.params.file)), m.get(`${c}/sdk/*file`, (o) => R(o, d, o.params.file)), m.get(`${c}/events`, (o) => {
155
+ o.type = "text/event-stream", o.set({ Connection: "keep-alive", "X-Accel-Buffering": "no" }), o.status = 200, o.method !== "HEAD" && (o.body = f.subscribe());
156
+ }), m.get("/favicon.ico", (o) => {
157
+ o.status = 204;
158
+ }), p.use(m.routes());
159
+ const w = p.listen(0, k);
160
+ try {
161
+ await I(w, "listening");
162
+ } catch (o) {
163
+ throw await f.close(), o;
164
+ }
165
+ const T = w.address();
166
+ let v;
167
+ return {
168
+ url: U({ protocol: "http:", hostname: k, port: T.port, pathname: "/" }),
169
+ close() {
170
+ return v ??= Promise.all([
171
+ f.close(),
172
+ new Promise((o, $) => {
173
+ w.close((y) => y ? $(y) : o());
174
+ })
175
+ ]).then(() => {
176
+ }), v;
177
+ }
178
+ };
179
+ }
180
+ export {
181
+ b as BusinessLine,
182
+ Z as startWorkbenchServer
183
+ };