@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.
- package/README.md +263 -2
- package/assets/phones/Nexus-5X.png +0 -0
- package/assets/phones/iPhone-14ProMax.png +0 -0
- package/assets/rednote-preview/cover/homebar-1439.jpg +0 -0
- package/assets/rednote-preview/cover/homebar-1679.jpg +0 -0
- package/assets/rednote-preview/cover/homebar-1919.jpg +0 -0
- package/assets/rednote-preview/cover/homebar-2284.jpg +0 -0
- package/assets/rednote-preview/cover/homebar-2285.jpg +0 -0
- package/assets/rednote-preview/cover/like.png +0 -0
- package/assets/rednote-preview/cover/play.png +0 -0
- package/assets/rednote-preview/cover/topbar-1439.jpg +0 -0
- package/assets/rednote-preview/cover/topbar-1679.jpg +0 -0
- package/assets/rednote-preview/cover/topbar-1919.jpg +0 -0
- package/assets/rednote-preview/cover/topbar-2284.jpg +0 -0
- package/assets/rednote-preview/cover/topbar-2285.jpg +0 -0
- package/assets/workbench/index.html +77 -0
- package/dist/sw/miniapp-simulator-service-worker.js +971 -0
- package/dist/sw/miniapp-simulator-service-worker.min.js +1 -0
- package/docs/consumer-guide.md +143 -0
- package/docs/electron-review.md +21 -0
- package/docs/external-cli-workbench.md +195 -0
- package/docs/file-system-design.md +334 -0
- package/docs/host-external-api.md +235 -0
- package/docs/iframe-service-worker.md +190 -0
- package/docs/local-dev.md +106 -0
- package/docs/npm-integration-verification.md +35 -0
- package/docs/remote-architecture.md +404 -0
- package/docs/remote-load-observability.md +268 -0
- package/docs/remote-usage.md +371 -0
- package/docs/request-design.md +247 -0
- package/docs/sdk-usage.md +260 -0
- package/docs/server-playground-https-debugging.md +150 -0
- package/docs/storage-api-design.md +275 -0
- package/docs/tracker.md +339 -0
- package/docs/worker-runtime.md +62 -0
- package/lib/business-line-CMb3ehRn.mjs +15 -0
- package/lib/client-CO-UjIY3.mjs +775 -0
- package/lib/index-B5oixYXr.mjs +574 -0
- package/lib/index.js +10763 -0
- package/lib/preload/webview-preload.js +37 -0
- package/lib/remote-client.js +11 -0
- package/lib/types/business-line.d.ts +7 -0
- package/lib/types/capabilities/constants.d.ts +29 -0
- package/lib/types/capabilities/csp-snippet.d.ts +34 -0
- package/lib/types/capabilities/launch-options.d.ts +7 -0
- package/lib/types/capabilities/performance-policy.d.ts +5 -0
- package/lib/types/capabilities/security-head-snippet.d.ts +21 -0
- package/lib/types/capabilities/security.d.ts +25 -0
- package/lib/types/capabilities/style-snippet.d.ts +2 -0
- package/lib/types/core/controllers/game-playtest.d.ts +9 -0
- package/lib/types/core/controllers/gesture.d.ts +8 -0
- package/lib/types/core/controllers/host-api.d.ts +19 -0
- package/lib/types/core/controllers/index.d.ts +11 -0
- package/lib/types/core/controllers/inspector.d.ts +20 -0
- package/lib/types/core/controllers/note-preview.d.ts +9 -0
- package/lib/types/core/controllers/permission-sheet.d.ts +7 -0
- package/lib/types/core/controllers/widget-cover.d.ts +12 -0
- package/lib/types/core/lifecycle.d.ts +7 -0
- package/lib/types/core/options.d.ts +33 -0
- package/lib/types/core/simulator.d.ts +9 -0
- package/lib/types/errors.d.ts +11 -0
- package/lib/types/event-bus.d.ts +15 -0
- package/lib/types/gesture/host-bus.d.ts +113 -0
- package/lib/types/gesture/index.d.ts +27 -0
- package/lib/types/gesture/plugins/double-tap.d.ts +10 -0
- package/lib/types/gesture/plugins/index.d.ts +27 -0
- package/lib/types/gesture/plugins/long-press.d.ts +13 -0
- package/lib/types/gesture/plugins/multi-touch-hint.d.ts +11 -0
- package/lib/types/gesture/plugins/multi-touch-switch.d.ts +18 -0
- package/lib/types/gesture/plugins/selection-disabler.d.ts +11 -0
- package/lib/types/gesture/plugins/tap.d.ts +11 -0
- package/lib/types/gesture/plugins/touch-emitter.d.ts +11 -0
- package/lib/types/gesture/plugins/wheel-scroll.d.ts +11 -0
- package/lib/types/gesture/receiver-runtime.d.ts +10 -0
- package/lib/types/gesture/receiver-runtime.impl.d.ts +6 -0
- package/lib/types/gesture/touch-cursor.d.ts +78 -0
- package/lib/types/gesture/touch-dot.d.ts +12 -0
- package/lib/types/gesture/types.d.ts +206 -0
- package/lib/types/host/shadow-host.d.ts +40 -0
- package/lib/types/index.d.ts +56 -0
- package/lib/types/injection/guest-log-mark.d.ts +7 -0
- package/lib/types/injection/head-builder.d.ts +4 -0
- package/lib/types/injection/index.d.ts +19 -0
- package/lib/types/injection/modules/console-forward.d.ts +2 -0
- package/lib/types/injection/modules/gesture.d.ts +6 -0
- package/lib/types/injection/modules/inspector-guest-runtime.d.ts +5 -0
- package/lib/types/injection/modules/inspector-pause-runtime.d.ts +6 -0
- package/lib/types/injection/modules/inspector.d.ts +8 -0
- package/lib/types/injection/modules/jsapi-channel.d.ts +9 -0
- package/lib/types/injection/modules/jsapi.d.ts +22 -0
- package/lib/types/injection/modules/render-done.d.ts +2 -0
- package/lib/types/injection/modules/resource-guard.d.ts +7 -0
- package/lib/types/injection/modules/scroll-containment.d.ts +3 -0
- package/lib/types/injection/modules/security.d.ts +3 -0
- package/lib/types/injection/modules/widget-snapshot.d.ts +13 -0
- package/lib/types/injection/preload-builder.d.ts +41 -0
- package/lib/types/injection/registry.d.ts +20 -0
- package/lib/types/injection/types.d.ts +45 -0
- package/lib/types/inspector/host-inspector.d.ts +108 -0
- package/lib/types/inspector/index.d.ts +8 -0
- package/lib/types/inspector/selector.d.ts +18 -0
- package/lib/types/native-ui/devtools/CapabilitiesView.d.ts +18 -0
- package/lib/types/native-ui/devtools/DevtoolsPanel.d.ts +24 -0
- package/lib/types/native-ui/devtools/LogsView.d.ts +17 -0
- package/lib/types/native-ui/devtools/dom.d.ts +40 -0
- package/lib/types/native-ui/devtools/index.d.ts +19 -0
- package/lib/types/native-ui/devtools/log-entry.d.ts +8 -0
- package/lib/types/native-ui/devtools/shortcut.d.ts +12 -0
- package/lib/types/native-ui/devtools/tokens.d.ts +14 -0
- package/lib/types/native-ui/devtools/types.d.ts +51 -0
- package/lib/types/native-ui/index.d.ts +17 -0
- package/lib/types/native-ui/note-publish/defaults.d.ts +40 -0
- package/lib/types/native-ui/note-publish/index.d.ts +2 -0
- package/lib/types/native-ui/note-publish/miniapp-note-publish.d.ts +15 -0
- package/lib/types/native-ui/note-publish/note-publish-host.d.ts +15 -0
- package/lib/types/native-ui/note-publish/view-data.d.ts +21 -0
- package/lib/types/native-ui/permission-sheet/defaults.d.ts +12 -0
- package/lib/types/native-ui/permission-sheet/index.d.ts +4 -0
- package/lib/types/native-ui/permission-sheet/miniapp-permission-sheet.d.ts +19 -0
- package/lib/types/native-ui/permission-sheet/permission-sheet-host.d.ts +23 -0
- package/lib/types/native-ui/permission-sheet/types.d.ts +8 -0
- package/lib/types/native-ui/rednote-preview/chrome-tone.d.ts +44 -0
- package/lib/types/native-ui/rednote-preview/defaults.d.ts +30 -0
- package/lib/types/native-ui/rednote-preview/half-sheet-drag.d.ts +61 -0
- package/lib/types/native-ui/rednote-preview/index.d.ts +2 -0
- package/lib/types/native-ui/rednote-preview/register.d.ts +7 -0
- package/lib/types/native-ui/rednote-preview/styles.d.ts +4 -0
- package/lib/types/native-ui/rednote-preview/xhs-note-detail-shell.d.ts +70 -0
- package/lib/types/native-ui/rednote-preview/xhs-note-half-sheet.d.ts +100 -0
- package/lib/types/native-ui/rednote-preview/xhs-profile-shell.d.ts +31 -0
- package/lib/types/native-ui/toast/frame-toast-host.d.ts +18 -0
- package/lib/types/native-ui/toast/index.d.ts +19 -0
- package/lib/types/presets.d.ts +11 -0
- package/lib/types/remote/client.d.ts +89 -0
- package/lib/types/remote/jsapi-config.d.ts +9 -0
- package/lib/types/remote/load-timing.d.ts +46 -0
- package/lib/types/remote/protocol.d.ts +165 -0
- package/lib/types/remote/review-launch.d.ts +6 -0
- package/lib/types/remote/runtime-entry.d.ts +1 -0
- package/lib/types/remote/runtime.d.ts +14 -0
- package/lib/types/remote/simulator-prompt.d.ts +9 -0
- package/lib/types/remote/widget-snapshot-guest-runtime.d.ts +1 -0
- package/lib/types/remote-client.d.ts +5 -0
- package/lib/types/renderer/base.d.ts +8 -0
- package/lib/types/renderer/content-host/iframe-content-host.d.ts +84 -0
- package/lib/types/renderer/content-host/index.d.ts +3 -0
- package/lib/types/renderer/content-host/types.d.ts +89 -0
- package/lib/types/renderer/content-host/webview-content-host.d.ts +53 -0
- package/lib/types/renderer/iframe.d.ts +93 -0
- package/lib/types/renderer/index.d.ts +2 -0
- package/lib/types/renderer/rednote-shell.d.ts +73 -0
- package/lib/types/renderer/webview.d.ts +59 -0
- package/lib/types/runtime/context.d.ts +31 -0
- package/lib/types/runtime/jsapi-modules/context.d.ts +19 -0
- package/lib/types/runtime/jsapi-modules/filesystem.d.ts +19 -0
- package/lib/types/runtime/jsapi-modules/index.d.ts +9 -0
- package/lib/types/runtime/jsapi-modules/interaction.d.ts +4 -0
- package/lib/types/runtime/jsapi-modules/location.d.ts +3 -0
- package/lib/types/runtime/jsapi-modules/media.d.ts +13 -0
- package/lib/types/runtime/jsapi-modules/network.d.ts +3 -0
- package/lib/types/runtime/jsapi-modules/storage.d.ts +26 -0
- package/lib/types/runtime/jsapi-modules/system.d.ts +4 -0
- package/lib/types/runtime/jsapi-modules/types.d.ts +21 -0
- package/lib/types/runtime/jsapi-modules/window-info.d.ts +43 -0
- package/lib/types/runtime/request/host-request.d.ts +13 -0
- package/lib/types/runtime/request/request-manager-frame.d.ts +29 -0
- package/lib/types/runtime/request/request-runtime.d.ts +26 -0
- package/lib/types/runtime/request/request-types.d.ts +26 -0
- package/lib/types/runtime/request/sse-text-framer.d.ts +16 -0
- package/lib/types/seal-config.d.ts +134 -0
- package/lib/types/sw/index.d.ts +9 -0
- package/lib/types/sw/parse-raw.d.ts +34 -0
- package/lib/types/sw/protocol.d.ts +19 -0
- package/lib/types/sw/served-entry.d.ts +9 -0
- package/lib/types/sw/service-worker-content-host.d.ts +11 -0
- package/lib/types/sw/service-worker-host.d.ts +74 -0
- package/lib/types/sw/service-worker-renderer.d.ts +10 -0
- package/lib/types/sw/worker/fs-codec.d.ts +23 -0
- package/lib/types/sw/worker/fs-ops.d.ts +22 -0
- package/lib/types/sw/worker/fs-store.d.ts +29 -0
- package/lib/types/sw/worker/globals.d.ts +50 -0
- package/lib/types/sw/worker/main.d.ts +6 -0
- package/lib/types/sw/worker/mime.d.ts +2 -0
- package/lib/types/sw/worker/mount-store.d.ts +48 -0
- package/lib/types/sw/worker/paths.d.ts +4 -0
- package/lib/types/sw/worker/serve.d.ts +13 -0
- package/lib/types/tracker/constants.d.ts +14 -0
- package/lib/types/tracker/default-tracker.d.ts +19 -0
- package/lib/types/tracker/index.d.ts +70 -0
- package/lib/types/transport/bridge-protocol.d.ts +78 -0
- package/lib/types/transport/iframe-transport.d.ts +39 -0
- package/lib/types/transport/index.d.ts +14 -0
- package/lib/types/transport/jsapi-bridge-messages.d.ts +36 -0
- package/lib/types/transport/types.d.ts +48 -0
- package/lib/types/transport/webview-transport.d.ts +52 -0
- package/lib/types/types/events.d.ts +130 -0
- package/lib/types/types/index.d.ts +11 -0
- package/lib/types/types/inspector.d.ts +172 -0
- package/lib/types/types/jsapi.d.ts +18 -0
- package/lib/types/types/options.d.ts +851 -0
- package/lib/types/types/renderer.d.ts +145 -0
- package/lib/types/utils/ancestor-scroll.d.ts +11 -0
- package/lib/types/utils/error-svg.d.ts +12 -0
- package/lib/types/utils/frameless-shell.d.ts +114 -0
- package/lib/types/utils/miniapp-chrome-svg.d.ts +47 -0
- package/lib/types/utils/package-constants.d.ts +8 -0
- package/lib/types/utils/screenshot.d.ts +14 -0
- package/lib/types/utils/simulator-pc-guard.d.ts +10 -0
- package/lib/types/utils/stage-fit.d.ts +6 -0
- package/lib/types/utils/touch-emulate.d.ts +26 -0
- package/lib/types/utils/zip-entry.d.ts +13 -0
- package/lib/types/workbench/index.d.ts +63 -0
- package/lib/types/workbench/preview-transition.d.ts +8 -0
- package/lib/types/workbench/server/artifact.d.ts +8 -0
- package/lib/types/workbench/server/index.d.ts +19 -0
- package/lib/types/workbench/server/watch.d.ts +6 -0
- package/lib/types/workbench/view.d.ts +36 -0
- package/lib/types/worker/adapters/csp-policy.d.ts +7 -0
- package/lib/types/worker/adapters/host-lifecycle.d.ts +5 -0
- package/lib/types/worker/adapters/mount-recovery.d.ts +2 -0
- package/lib/types/worker/adapters/package-policy.d.ts +12 -0
- package/lib/types/worker/adapters/page-lifecycle.d.ts +1 -0
- package/lib/types/worker/adapters/protocol.d.ts +49 -0
- package/lib/types/worker/adapters/scripts.d.ts +2 -0
- package/lib/types/worker/adapters/service-worker.d.ts +2 -0
- package/lib/workbench-server.mjs +183 -0
- package/lib/workbench.js +1404 -0
- package/package.json +61 -4
- 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,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
|
+
};
|