@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.
- 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 +408 -0
- package/docs/remote-load-observability.md +268 -0
- package/docs/remote-usage.md +376 -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/client-BlQ9j4FW.mjs +775 -0
- package/lib/index-DI2oETZ_.mjs +574 -0
- package/lib/index.js +10831 -0
- package/lib/options-B34ipLmF.mjs +15 -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/phone-vibration.d.ts +10 -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-alert.d.ts +3 -0
- package/lib/types/runtime/jsapi-modules/context.d.ts +21 -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,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ServiceWorkerHost —— `renderer: "iframe-service-worker"` 的宿主侧管理器。
|
|
3
|
+
*
|
|
4
|
+
* 负责:注册 SW(按 scope 单例幂等)、把解压后的文件表 MOUNT 给 SW、生成可直连的
|
|
5
|
+
* 同源虚拟 URL、UNMOUNT 回收。仅依赖浏览器 API(无 Node 依赖,可进 UMD 构建)。
|
|
6
|
+
*/
|
|
7
|
+
import { type ServiceWorkerFileSystemArgs, type ServiceWorkerFileSystemOperation } from "./protocol";
|
|
8
|
+
/** 是否具备启用 SW 模式的运行环境(API 可用 + 安全上下文)。 */
|
|
9
|
+
export declare function isServiceWorkerHostSupported(): boolean;
|
|
10
|
+
export interface ServiceWorkerMountInput {
|
|
11
|
+
worker?: import("../worker/adapters/package-policy").WorkerMountPolicy;
|
|
12
|
+
id: string;
|
|
13
|
+
/** 入口文件包内路径(如 `index.html`),用于生成虚拟 URL。 */
|
|
14
|
+
entryPath: string;
|
|
15
|
+
/** 包内全部文件:路径 → 原始字节(会被 transfer 给 SW,调用后不可再用)。 */
|
|
16
|
+
rawFiles: Record<string, ArrayBuffer>;
|
|
17
|
+
/** 注入到 HTML `<head>` 的片段(SW 在返回页面时改写写入)。 */
|
|
18
|
+
injectHead: string;
|
|
19
|
+
/** CSP 请求白名单(SW 作为响应头下发)。 */
|
|
20
|
+
csp: string;
|
|
21
|
+
/** 可选:远程回源基址(入口目录 URL)。传入即为远程代理实例,未挂载子资源回源到此(远程需开 CORS)。 */
|
|
22
|
+
remoteBase?: string;
|
|
23
|
+
/** 可选:稳定存储桶身份(= appId)。传入则 writeTempFile 落 IDB 持久化、返回跨会话可读 URL。 */
|
|
24
|
+
fsScope?: string;
|
|
25
|
+
}
|
|
26
|
+
/** simulator 使用的 SW 宿主最小接口。 */
|
|
27
|
+
export interface ServiceWorkerHostLike {
|
|
28
|
+
readonly scope: string;
|
|
29
|
+
fileUrl(fsScope: string, path: string): string;
|
|
30
|
+
mount(input: ServiceWorkerMountInput): Promise<string>;
|
|
31
|
+
restoreMount(id: string): Promise<void>;
|
|
32
|
+
fsInvoke(id: string, op: ServiceWorkerFileSystemOperation, args?: ServiceWorkerFileSystemArgs): Promise<unknown>;
|
|
33
|
+
unmount(id: string): void;
|
|
34
|
+
}
|
|
35
|
+
export declare class ServiceWorkerHost {
|
|
36
|
+
private sw;
|
|
37
|
+
readonly scope: string;
|
|
38
|
+
private readonly mountTimeout;
|
|
39
|
+
private registration;
|
|
40
|
+
private readonly scriptUrl;
|
|
41
|
+
private static readonly instances;
|
|
42
|
+
/** 当前 host 已 MOUNT 的 payload 快照;SW 被外部注销/重启后用于重新挂载。 */
|
|
43
|
+
private readonly mountSnapshots;
|
|
44
|
+
private supportedFsOps;
|
|
45
|
+
private constructor();
|
|
46
|
+
/** 生成与 SW `_xhsfile_` fetch 命名空间完全一致的同源文件 URL。 */
|
|
47
|
+
fileUrl(fsScope: string, path: string): string;
|
|
48
|
+
/** 单例获取(固定 scope 幂等注册并等待 SW 激活)。 */
|
|
49
|
+
static acquire(scriptUrl: string, mountTimeout?: number, scope?: string): Promise<ServiceWorkerHost>;
|
|
50
|
+
private static create;
|
|
51
|
+
/**
|
|
52
|
+
* 挂载一个预览实例,返回可直连的同源虚拟 URL(`<scope><id>/<entryPath>`)。
|
|
53
|
+
* SW 在返回 HTML 时按 `injectHead` 改写 `<head>`、按 `csp` 下发响应头。
|
|
54
|
+
*/
|
|
55
|
+
mount(input: ServiceWorkerMountInput): Promise<string>;
|
|
56
|
+
/** 重新挂载已知实例:用于 iframe 被卸载后再次加载虚拟 URL 前恢复 SW 代理。 */
|
|
57
|
+
restoreMount(id: string): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* 文件系统调用(host→SW):按 `op` 分派并返回 op 结果。
|
|
60
|
+
*/
|
|
61
|
+
fsInvoke(id: string, op: ServiceWorkerFileSystemOperation, args?: ServiceWorkerFileSystemArgs): Promise<unknown>;
|
|
62
|
+
private sendFsRequest;
|
|
63
|
+
unmount(id: string): void;
|
|
64
|
+
private postWithAck;
|
|
65
|
+
private sendMount;
|
|
66
|
+
/**
|
|
67
|
+
* MOUNT 前自检:确保仍持有【我们自己】的、可用的 SW。
|
|
68
|
+
* - 注册仍在 → 刷新 this.sw / this.registration 到当前活跃 worker。
|
|
69
|
+
* - 注册被外部注销 → 重新注册并等待激活,刷新引用。
|
|
70
|
+
*/
|
|
71
|
+
private ensureActiveWorker;
|
|
72
|
+
/** 校验注册确属我们的实例(scope 精确匹配),排除宿主根 scope SW 的最长前缀回退命中。 */
|
|
73
|
+
private isOurRegistration;
|
|
74
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ContentHostContext } from "../renderer/content-host/types";
|
|
2
|
+
import { IframeRendererAdapter } from "../renderer/iframe";
|
|
3
|
+
import { ServiceWorkerContentHost } from "./service-worker-content-host";
|
|
4
|
+
/**
|
|
5
|
+
* SW 模式渲染器:完全复用 IframeRendererAdapter(设备外壳 / rednote 壳层 / 手势 /
|
|
6
|
+
* 选取 / 权限弹窗),仅把内容载体替换为 {@link ServiceWorkerContentHost}。
|
|
7
|
+
*/
|
|
8
|
+
export declare class ServiceWorkerRendererAdapter extends IframeRendererAdapter {
|
|
9
|
+
protected createContentHost(ctx: ContentHostContext): ServiceWorkerContentHost;
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fs-codec —— data: URI 解码 + MIME 白名单(**零依赖纯逻辑**,仅用 `atob`/`Uint8Array`)。
|
|
3
|
+
*
|
|
4
|
+
* 文件系统层通用工具,供各 op 复用(当前 write,未来 read 等),非某个 jsapi 专属。
|
|
5
|
+
* 只接受 base64 `data:` URI,MIME 必须在白名单内;其余(裸 base64 / http(s) / 不支持 MIME /
|
|
6
|
+
* 坏 base64)一律拒绝。白名单对齐端上「小工具本地文件能力」一期(图片 + 视频)。
|
|
7
|
+
*/
|
|
8
|
+
/** 文件系统支持的 MIME → 扩展名白名单(一期图片 + 视频)。 */
|
|
9
|
+
export declare const SW_MIME_EXT: Record<string, string>;
|
|
10
|
+
export type DecodeErrorCode = "ERR_INVALID_ARGS" | "ERR_UNSUPPORTED_MIME";
|
|
11
|
+
export interface DecodeOk {
|
|
12
|
+
bytes: ArrayBuffer;
|
|
13
|
+
mime: string;
|
|
14
|
+
ext: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DecodeErr {
|
|
17
|
+
error: string;
|
|
18
|
+
code: DecodeErrorCode;
|
|
19
|
+
}
|
|
20
|
+
/** 通过文件魔数识别可由 DOM 直连渲染的媒体类型。 */
|
|
21
|
+
export declare function sniffMediaMime(buffer: ArrayBuffer): string | undefined;
|
|
22
|
+
/** 解析并解码 `data:<mime>;base64,<body>`。成功返回字节+mime+ext;失败返回错误码。 */
|
|
23
|
+
export declare function decodeDataUri(input: unknown): DecodeOk | DecodeErr;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Runtime file system operations and `_xhsfile_` URL serving. */
|
|
2
|
+
import type { ServiceWorkerFileSystemArgs, ServiceWorkerFileSystemOperation } from "../protocol";
|
|
3
|
+
export declare const FS_USR_LIMIT_BYTES: number;
|
|
4
|
+
export declare const FS_WRITE_CHUNK_LIMIT_BYTES: number;
|
|
5
|
+
export declare const FS_READ_CHUNK_LIMIT_BYTES: number;
|
|
6
|
+
export declare const FS_SESSION_WRITE_LIMIT_BYTES: number;
|
|
7
|
+
export declare const FS_MAX_DEPTH = 4;
|
|
8
|
+
export declare const FS_PATH_MAX_BYTES = 255;
|
|
9
|
+
export interface FsMessage extends ServiceWorkerFileSystemArgs {
|
|
10
|
+
op: ServiceWorkerFileSystemOperation;
|
|
11
|
+
id: string;
|
|
12
|
+
reqId: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function releaseFsSession(id: string): void;
|
|
15
|
+
export declare function handleFsOp(data: FsMessage): Promise<Record<string, unknown>>;
|
|
16
|
+
/** Parse a simulator runtime-file URL for the fetch interception path. */
|
|
17
|
+
export declare function parseXhsFileUrl(rawUrl: string): {
|
|
18
|
+
scope: string;
|
|
19
|
+
path: string;
|
|
20
|
+
} | null;
|
|
21
|
+
/** Serve media files to img/video/CSS; non-media bytes intentionally return 404. */
|
|
22
|
+
export declare function serveXhsFile(scope: string, path: string): Promise<Response>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** IndexedDB-backed runtime file store used by the simulator Service Worker. */
|
|
2
|
+
export interface StoredFile {
|
|
3
|
+
path: string;
|
|
4
|
+
bytes: ArrayBuffer;
|
|
5
|
+
mime: string;
|
|
6
|
+
isDir: boolean;
|
|
7
|
+
lastModified: number;
|
|
8
|
+
}
|
|
9
|
+
export interface StoredEntry {
|
|
10
|
+
path: string;
|
|
11
|
+
mime: string;
|
|
12
|
+
size: number;
|
|
13
|
+
isDir: boolean;
|
|
14
|
+
lastModified: number;
|
|
15
|
+
}
|
|
16
|
+
/** 写入或覆盖普通文件。 */
|
|
17
|
+
export declare function idbPut(scope: string, path: string, bytes: ArrayBuffer, mime?: string): Promise<void>;
|
|
18
|
+
/** 创建目录记录。 */
|
|
19
|
+
export declare function idbPutDirectory(scope: string, path: string): Promise<void>;
|
|
20
|
+
/** 读取单个条目;不存在返回 null。 */
|
|
21
|
+
export declare function idbGet(scope: string, path: string): Promise<StoredFile | null>;
|
|
22
|
+
/** 列出 scope 下的全部文件与目录元信息。 */
|
|
23
|
+
export declare function idbList(scope: string): Promise<StoredEntry[]>;
|
|
24
|
+
/** 删除单个条目。 */
|
|
25
|
+
export declare function idbDelete(scope: string, path: string): Promise<void>;
|
|
26
|
+
/** 在同一个事务中移动文件,目标由调用方保证不存在。 */
|
|
27
|
+
export declare function idbMove(scope: string, sourcePath: string, targetPath: string, source: StoredFile): Promise<void>;
|
|
28
|
+
/** 删除某个 scope 下的全部条目。 */
|
|
29
|
+
export declare function idbDeleteScope(scope: string): Promise<void>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service Worker 全局最小类型 + `sw` 句柄。
|
|
3
|
+
*
|
|
4
|
+
* 用最小本地类型描述 SW 全局,避免与项目 DOM lib 的全局类型冲突(无需引入 `webworker` lib)。
|
|
5
|
+
* 仅 worker 运行时内部使用。
|
|
6
|
+
*/
|
|
7
|
+
export interface SwExtendableEvent {
|
|
8
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
9
|
+
}
|
|
10
|
+
export interface SwMessageEvent {
|
|
11
|
+
data: unknown;
|
|
12
|
+
ports: ReadonlyArray<MessagePort>;
|
|
13
|
+
source?: SwClient | MessagePort | null;
|
|
14
|
+
/** ExtendableMessageEvent.waitUntil:异步 op(FS)期间保活 SW。 */
|
|
15
|
+
waitUntil?(promise: Promise<unknown>): void;
|
|
16
|
+
}
|
|
17
|
+
export interface SwFetchEvent {
|
|
18
|
+
request: Request;
|
|
19
|
+
/** 发起该请求的文档/worker client id(子资源请求时指向所属文档)。 */
|
|
20
|
+
clientId: string;
|
|
21
|
+
/** 导航请求新建 client 的 id(文档自身导航时用)。 */
|
|
22
|
+
resultingClientId: string;
|
|
23
|
+
respondWith(response: Response | Promise<Response>): void;
|
|
24
|
+
}
|
|
25
|
+
export interface SwClient {
|
|
26
|
+
id: string;
|
|
27
|
+
url: string;
|
|
28
|
+
postMessage(message: unknown, transfer: Transferable[]): void;
|
|
29
|
+
}
|
|
30
|
+
export interface SwClients {
|
|
31
|
+
claim(): Promise<void>;
|
|
32
|
+
get(id: string): Promise<SwClient | undefined>;
|
|
33
|
+
matchAll(options?: {
|
|
34
|
+
type?: "window";
|
|
35
|
+
includeUncontrolled?: boolean;
|
|
36
|
+
}): Promise<SwClient[]>;
|
|
37
|
+
}
|
|
38
|
+
export interface SwGlobalScope {
|
|
39
|
+
skipWaiting(): Promise<void>;
|
|
40
|
+
clients: SwClients;
|
|
41
|
+
/** 本 SW 注册 scope(完整 URL,如 `https://origin/miniapp-simulator/`),用于拼同源虚拟 URL。 */
|
|
42
|
+
registration: {
|
|
43
|
+
scope: string;
|
|
44
|
+
};
|
|
45
|
+
addEventListener(type: "install", listener: (event: SwExtendableEvent) => void): void;
|
|
46
|
+
addEventListener(type: "activate", listener: (event: SwExtendableEvent) => void): void;
|
|
47
|
+
addEventListener(type: "message", listener: (event: SwMessageEvent) => void): void;
|
|
48
|
+
addEventListener(type: "fetch", listener: (event: SwFetchEvent) => void): void;
|
|
49
|
+
}
|
|
50
|
+
export declare const sw: SwGlobalScope;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 挂载表 —— id → 已挂载小工具实例。多预览实例 / 多 iframe 共用一个 SW,按 id 前缀隔离。
|
|
3
|
+
* 负责 MOUNT/UNMOUNT 的存储,以及请求路径 → 实例的两级归属查找(`/<id>/` 标记 + clientId 反查)。
|
|
4
|
+
*/
|
|
5
|
+
import type { WorkerMountPolicy } from "../../worker/adapters/package-policy";
|
|
6
|
+
export interface MountedFile {
|
|
7
|
+
bytes: ArrayBuffer;
|
|
8
|
+
mime: string;
|
|
9
|
+
}
|
|
10
|
+
export interface MountedApp {
|
|
11
|
+
worker?: WorkerMountPolicy;
|
|
12
|
+
/** 以包内文件路径为键缓存原始字节及独立 CSP 响应。 */
|
|
13
|
+
workerResponses: Map<string, Response>;
|
|
14
|
+
files: Map<string, MountedFile>;
|
|
15
|
+
/** 注入到 HTML `<head>` 的片段(seal/jsapi/render-done 等)。 */
|
|
16
|
+
injectHead: string;
|
|
17
|
+
/** 作为响应头下发的 CSP(请求白名单)。 */
|
|
18
|
+
csp: string;
|
|
19
|
+
/** 远程回源基址(入口目录 URL)。非空=远程代理实例:子资源未命中回源到此;空=纯离线包,未命中 404。 */
|
|
20
|
+
remoteBase: string;
|
|
21
|
+
/** 稳定存储桶身份(= host 传入的 appId);空串时由调用方回退到 mount id。 */
|
|
22
|
+
fsScope: string;
|
|
23
|
+
/** 发起 MOUNT 的页面 Client;页面异常退出时用于回收没有机会发送 UNMOUNT 的孤儿挂载。 */
|
|
24
|
+
ownerClientId: string;
|
|
25
|
+
}
|
|
26
|
+
export declare const STORE: Map<string, MountedApp>;
|
|
27
|
+
export interface MountInput {
|
|
28
|
+
worker?: WorkerMountPolicy;
|
|
29
|
+
id: string;
|
|
30
|
+
files: Record<string, ArrayBuffer>;
|
|
31
|
+
injectHead: string;
|
|
32
|
+
csp: string;
|
|
33
|
+
remoteBase?: string;
|
|
34
|
+
fsScope?: string;
|
|
35
|
+
ownerClientId?: string;
|
|
36
|
+
}
|
|
37
|
+
/** 挂载一个实例,返回文件数。 */
|
|
38
|
+
export declare function mountApp(input: MountInput): number;
|
|
39
|
+
export declare function unmountApp(id: string): void;
|
|
40
|
+
/** 清理所属页面已不存在的挂载;无 owner 的旧协议挂载保留,避免误删仍在运行的实例。 */
|
|
41
|
+
export declare function pruneOrphanedApps(activeClientIds: ReadonlySet<string>): number;
|
|
42
|
+
/** 把请求 URL 解析为「命中的挂载实例 + 包内文件路径」。仅按 pathname 匹配,忽略 query。 */
|
|
43
|
+
export declare function resolveRequest(rawUrl: string): {
|
|
44
|
+
app: MountedApp;
|
|
45
|
+
filePath: string;
|
|
46
|
+
} | null;
|
|
47
|
+
/** 按 client 文档 URL 反查其所属的已挂载实例(url 含 `/<id>/`)。 */
|
|
48
|
+
export declare function findAppByClientUrl(clientUrl: string): MountedApp | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 响应构造 —— 把命中的包内文件落成 Response。
|
|
3
|
+
* · serveDirect:`/<id>/` 标记命中直供;远程代理实例未命中则回源。
|
|
4
|
+
* · serveByClient:根绝对路径 `/assets/x`(无标记)按 clientId 反查归属实例再供/回源/透传。
|
|
5
|
+
* · serveBytes:裸字节 → Response(供运行期文件 `_xhsfile_` 复用)。
|
|
6
|
+
*/
|
|
7
|
+
import { type MountedApp } from "./mount-store";
|
|
8
|
+
/** 命中实例取本地文件;远程代理实例未命中则按相对路径回源,纯离线包未命中 404。 */
|
|
9
|
+
export declare function serveDirect(app: MountedApp, filePath: string, request: Request): Response | Promise<Response>;
|
|
10
|
+
/** 根绝对路径回退:按 clientId 反查归属实例,命中文件则供,有 remoteBase 则回源,否则透传。 */
|
|
11
|
+
export declare function serveByClient(clientId: string, request: Request): Promise<Response>;
|
|
12
|
+
/** 裸字节 → Response(no-store)。供包内二进制文件与运行期 `_xhsfile_` 文件共用。 */
|
|
13
|
+
export declare function serveBytes(bytes: ArrayBuffer, mime: string): Response;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MiniAppApmRegion } from "../types";
|
|
2
|
+
/** APM 自定义打点的 measurement_name,所有 tracker 上报统一使用 */
|
|
3
|
+
export declare const APM_MEASUREMENT_NAME = "miniapp_simulator_event";
|
|
4
|
+
/** APM 应用标识(context_artifactName),后端按此归类本 SDK 的上报 */
|
|
5
|
+
export declare const APM_ARTIFACT_NAME = "skillbuilder-miniapp";
|
|
6
|
+
/** 按 region + 环境分的 APM xray 上报端点;可由 tracker.endpoint 覆盖 */
|
|
7
|
+
export declare const APM_ENDPOINT: Record<MiniAppApmRegion, {
|
|
8
|
+
production: string;
|
|
9
|
+
development: string;
|
|
10
|
+
}>;
|
|
11
|
+
/** 批量上报:缓冲达到此条数立即发送 */
|
|
12
|
+
export declare const APM_BATCH_MAX = 10;
|
|
13
|
+
/** 批量上报:缓冲未满时最多等待此毫秒后发送 */
|
|
14
|
+
export declare const APM_BATCH_FLUSH_MS = 10000;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { MiniAppApmRegion, MiniAppTrackerEvent } from "../types";
|
|
2
|
+
/** 默认 Tracker 的上报方法集合 */
|
|
3
|
+
export interface DefaultTracker {
|
|
4
|
+
sendCustomPoint(event: MiniAppTrackerEvent): void;
|
|
5
|
+
/** 立即发送缓冲中的所有打点 */
|
|
6
|
+
flush(): void;
|
|
7
|
+
}
|
|
8
|
+
export interface DefaultTrackerConfig {
|
|
9
|
+
/** xray 上报端点 URL */
|
|
10
|
+
endpoint: string;
|
|
11
|
+
/** 上报地域,注入 context_accountGeographicZone */
|
|
12
|
+
region: MiniAppApmRegion;
|
|
13
|
+
/** 异步获取当前用户 ID(来自 tracker 选项的 `getUserInfo`),注入 context_userId */
|
|
14
|
+
resolveUserId: () => Promise<string>;
|
|
15
|
+
/** 打印到 console 而非真正上报;仅显式传 true 时开启。 */
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** 创建默认 Tracker:自定义点位缓冲后批量 POST 到 APM 端点 */
|
|
19
|
+
export declare function createDefaultTracker(config: DefaultTrackerConfig): DefaultTracker;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { EventBus } from "../event-bus";
|
|
2
|
+
import type { BridgeInvokeResult } from "../transport";
|
|
3
|
+
import type { MiniAppTrackerInput, MiniAppTrackerOptions } from "../types";
|
|
4
|
+
/** 解析上报端点:`endpoint` 优先,否则只按当前域名选测试/正式端点。 */
|
|
5
|
+
export declare function resolveTrackerEndpoint(input: MiniAppTrackerOptions): string;
|
|
6
|
+
export declare class MiniAppTracker {
|
|
7
|
+
private readonly resolveContext?;
|
|
8
|
+
private readonly send;
|
|
9
|
+
private readonly flush;
|
|
10
|
+
private readonly unsubscribers;
|
|
11
|
+
/** 公共字段,init 时设置,后续所有打点自动注入 */
|
|
12
|
+
private common;
|
|
13
|
+
constructor(input: MiniAppTrackerInput, eventBus: EventBus, resolveUserId?: () => Promise<string>, resolveContext?: (() => Record<string, string | number | undefined>) | undefined);
|
|
14
|
+
/** 页面隐藏时立即冲刷缓冲,避免关闭前丢数据 */
|
|
15
|
+
private bindFlushOnHidden;
|
|
16
|
+
private bindEvents;
|
|
17
|
+
/** 初始化公共字段并上报 simulator_init */
|
|
18
|
+
trackInit(params: {
|
|
19
|
+
renderer: string;
|
|
20
|
+
contentMode: string;
|
|
21
|
+
appUrl: string;
|
|
22
|
+
sdkVersion: string;
|
|
23
|
+
}): void;
|
|
24
|
+
/** 上报 reload 事件 */
|
|
25
|
+
trackReload(url: string): void;
|
|
26
|
+
/** srcdoc 注入完成 */
|
|
27
|
+
trackInjectDone(params: {
|
|
28
|
+
url: string;
|
|
29
|
+
injected: boolean;
|
|
30
|
+
duration: number;
|
|
31
|
+
}): void;
|
|
32
|
+
/** 权限弹窗操作结果 */
|
|
33
|
+
trackPermissionResult(params: {
|
|
34
|
+
type: string;
|
|
35
|
+
result: string;
|
|
36
|
+
}): void;
|
|
37
|
+
/** zip/files 解析完成 */
|
|
38
|
+
trackParseDone(params: {
|
|
39
|
+
type: string;
|
|
40
|
+
duration: number;
|
|
41
|
+
count: number;
|
|
42
|
+
entry?: string;
|
|
43
|
+
}): void;
|
|
44
|
+
/** SW 注册 + 激活结果(renderer: iframe-service-worker);scope 复用 url 字段 */
|
|
45
|
+
trackSwRegister(params: {
|
|
46
|
+
result: "success" | "fail";
|
|
47
|
+
duration: number;
|
|
48
|
+
url?: string;
|
|
49
|
+
message?: string;
|
|
50
|
+
}): void;
|
|
51
|
+
/** SW MOUNT(文件表挂载 + ACK)结果 */
|
|
52
|
+
trackSwMount(params: {
|
|
53
|
+
result: "success" | "fail";
|
|
54
|
+
duration: number;
|
|
55
|
+
count?: number;
|
|
56
|
+
entry?: string;
|
|
57
|
+
message?: string;
|
|
58
|
+
}): void;
|
|
59
|
+
/** 远程加载内部阶段;stage 保持低基数枚举,扩展字段只承载耗时与结果。 */
|
|
60
|
+
trackLoadStage(stage: string, params: Record<string, string | number | boolean | undefined>): void;
|
|
61
|
+
/** bridge 调用完成回调 */
|
|
62
|
+
handleBridgeInvoke(result: BridgeInvokeResult): void;
|
|
63
|
+
/** 发送打点:自动合入公共字段 */
|
|
64
|
+
private track;
|
|
65
|
+
destroy(): void;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 判断 tracker input 是否启用。
|
|
69
|
+
*/
|
|
70
|
+
export declare function isTrackerEnabled(input?: MiniAppTrackerInput): input is MiniAppTrackerOptions;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Transport } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* BridgeProtocol —— Simulator 宿主侧 jsapi 协议解析器(介质无关)
|
|
4
|
+
*
|
|
5
|
+
* 本类只面向 {@link Transport} 接口编程,与 iframe / webview 介质完全解耦。
|
|
6
|
+
*
|
|
7
|
+
* 职责:
|
|
8
|
+
* 1. 监听 transport 来的消息,识别 `xhs:invoke` 调用并路由到对应 handler
|
|
9
|
+
* 2. 把 handler 返回值包装成 `{ code: 0, data }` 或 `{ code, msg }`,
|
|
10
|
+
* 通过 `transport.post(callbackMessage)` 异步回写
|
|
11
|
+
* 3. 主动推送事件 `emit(method, payload)` → `xhs:event` 消息
|
|
12
|
+
* 4. 处理模拟器通道 `ready` 心跳(仅 debug 时记录)
|
|
13
|
+
*
|
|
14
|
+
* 详见:design/mini-app/mini-app-simulator-refactor-design.md §6
|
|
15
|
+
*/
|
|
16
|
+
/** Handler 入参:jsapi 调用解析后的协议字段 */
|
|
17
|
+
export interface BridgeRequest {
|
|
18
|
+
/** jsapi event 名(如 `share` / `xhsRequest`) */
|
|
19
|
+
event: string;
|
|
20
|
+
/** sdk 传给 invoke 的业务参数(不含 success/fail/complete) */
|
|
21
|
+
message: Record<string, unknown>;
|
|
22
|
+
/** callbackId,可用于自定义异步回调时序 */
|
|
23
|
+
callbackId: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Handler 返回的"业务数据"会被 BridgeProtocol 包装成 `{ code: 0, data }` 回写。
|
|
27
|
+
* 抛错(同步或 reject)将被视为失败,包装成 `{ code, msg }`。
|
|
28
|
+
*/
|
|
29
|
+
export type BridgeHandler = (request: BridgeRequest) => unknown | Promise<unknown>;
|
|
30
|
+
/** bridge 调用结果回调(Tracker 用) */
|
|
31
|
+
export interface BridgeInvokeResult {
|
|
32
|
+
event: string;
|
|
33
|
+
success: boolean;
|
|
34
|
+
duration: number;
|
|
35
|
+
error?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface BridgeProtocolOptions {
|
|
38
|
+
/** 默认延迟(毫秒),模拟端到端往返;默认 60 */
|
|
39
|
+
defaultDelay?: number;
|
|
40
|
+
/** 调试日志开关 */
|
|
41
|
+
debug?: boolean;
|
|
42
|
+
/** bridge 调用完成回调(成功/失败),供 Tracker 上报 */
|
|
43
|
+
onInvoke?: (result: BridgeInvokeResult) => void;
|
|
44
|
+
}
|
|
45
|
+
export declare class BridgeProtocol {
|
|
46
|
+
private readonly transport;
|
|
47
|
+
private readonly handlers;
|
|
48
|
+
private readonly defaultDelay;
|
|
49
|
+
private fallbackHandler;
|
|
50
|
+
private readonly debug;
|
|
51
|
+
private readonly onInvoke?;
|
|
52
|
+
private readonly unsubscribe;
|
|
53
|
+
private destroyed;
|
|
54
|
+
constructor(transport: Transport, options?: BridgeProtocolOptions);
|
|
55
|
+
/** Simulator 内部设置未注册事件的最终路由。 */
|
|
56
|
+
setFallbackHandler(handler: BridgeHandler): void;
|
|
57
|
+
/** 注册(或覆盖)单个 event 的 handler,返回 unsubscribe */
|
|
58
|
+
handle(event: string, handler: BridgeHandler): () => void;
|
|
59
|
+
/**
|
|
60
|
+
* 批量注册 handler map。常用于 `options.hostApi` 入口。
|
|
61
|
+
* 同名 event 会覆盖之前注册的 handler。
|
|
62
|
+
*/
|
|
63
|
+
register(map: Record<string, BridgeHandler>): void;
|
|
64
|
+
/** 是否已注册 handler */
|
|
65
|
+
has(event: string): boolean;
|
|
66
|
+
/** 移除单个 event 的 handler */
|
|
67
|
+
remove(event: string): void;
|
|
68
|
+
/** 清空所有精确 handler(保留内部 fallbackHandler) */
|
|
69
|
+
clear(): void;
|
|
70
|
+
/**
|
|
71
|
+
* 主动向小应用推送事件(触发 jsapi 的 `subscribe(method, ...)` 监听)。
|
|
72
|
+
* payload 形态符合 `{ type: "events", method, payload }`,本方法做自动包装。
|
|
73
|
+
*/
|
|
74
|
+
emit(method: string, payload?: unknown): void;
|
|
75
|
+
destroy(): void;
|
|
76
|
+
private readonly handleMessage;
|
|
77
|
+
private dispatch;
|
|
78
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Transport, TransportMessage } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* IframeTransport —— iframe 介质的 Transport 实现。
|
|
4
|
+
*
|
|
5
|
+
* 数据流:
|
|
6
|
+
* 宿主 → 小应用: this.iframe.contentWindow.postMessage(msg, iframeOrigin)
|
|
7
|
+
* 小应用 → 宿主: 校验 source + origin 后分发
|
|
8
|
+
*
|
|
9
|
+
* 设计说明:
|
|
10
|
+
* - 跨站 OOPIF 模式下必须只接收当前 iframe 的消息,并使用精确 targetOrigin,避免其它
|
|
11
|
+
* frame 伪造 bridge 调用。默认 sandbox 含 allow-same-origin,因此 event.origin 可稳定校验。
|
|
12
|
+
* - `ready` 在 `markReady()` 之前是 pending 状态;renderer 在 `iframe.onload`
|
|
13
|
+
* 触发后调用 `markReady()`。
|
|
14
|
+
*
|
|
15
|
+
* 注:本类**不感知具体业务消息**(jsapi/console/navigation/render-done),
|
|
16
|
+
* 它只负责字节搬运。业务路由由上层模块完成。
|
|
17
|
+
*/
|
|
18
|
+
export declare class IframeTransport implements Transport {
|
|
19
|
+
readonly ready: Promise<void>;
|
|
20
|
+
private readyResolve;
|
|
21
|
+
private iframe;
|
|
22
|
+
private readonly handlers;
|
|
23
|
+
private destroyed;
|
|
24
|
+
constructor();
|
|
25
|
+
/**
|
|
26
|
+
* 将 Transport 绑定到目标 iframe 元素。
|
|
27
|
+
* 调用之前 `post` 会被静默丢弃。
|
|
28
|
+
*/
|
|
29
|
+
attach(element: HTMLElement): void;
|
|
30
|
+
/**
|
|
31
|
+
* Renderer 在 iframe.onload(或 srcdoc load done)后调用,触发 ready。
|
|
32
|
+
* 重复调用幂等。
|
|
33
|
+
*/
|
|
34
|
+
markReady(): void;
|
|
35
|
+
post(message: TransportMessage): void;
|
|
36
|
+
onMessage(handler: (msg: TransportMessage) => void): () => void;
|
|
37
|
+
destroy(): void;
|
|
38
|
+
private readonly handleMessage;
|
|
39
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport 层统一出口(介质相关通道 + 协议)。
|
|
3
|
+
*
|
|
4
|
+
* - `Transport / TransportMessage`:介质无关的字节通道接口
|
|
5
|
+
* - `IframeTransport / WebviewTransport`:两种介质的实现
|
|
6
|
+
* - `BridgeProtocol / BridgeHandler / BridgeRequest`:jsapi 协议解析器
|
|
7
|
+
*
|
|
8
|
+
* Kernel 与 Plugin 内部使用本文件统一导入;不对宿主暴露(不在
|
|
9
|
+
* [`src/index.ts`](../index.ts) 中 re-export)。
|
|
10
|
+
*/
|
|
11
|
+
export { IframeTransport } from "./iframe-transport";
|
|
12
|
+
export { WebviewTransport, WEBVIEW_IPC_CHANNEL } from "./webview-transport";
|
|
13
|
+
export type { Transport, TransportMessage } from "./types";
|
|
14
|
+
export { BridgeProtocol, type BridgeHandler, type BridgeInvokeResult, type BridgeProtocolOptions, type BridgeRequest, } from "./bridge-protocol";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simulator host ↔ guest JS API bridge message contract.
|
|
3
|
+
*
|
|
4
|
+
* This contract belongs to the simulator because it describes its transport,
|
|
5
|
+
* rather than the client JS API's native bridge contract.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SIMULATOR_JSAPI_MESSAGE_TYPES: {
|
|
8
|
+
/** guest → host: the simulator bridge is ready */
|
|
9
|
+
readonly ready: "designable-small-tool:xhs:ready";
|
|
10
|
+
/** guest → host: an SDK call forwarded through xhsInvoke */
|
|
11
|
+
readonly invoke: "designable-small-tool:xhs:invoke";
|
|
12
|
+
/** host → guest: an asynchronous SDK result */
|
|
13
|
+
readonly invokeCallback: "designable-small-tool:xhs:invoke:callback";
|
|
14
|
+
/** host → guest: a pushed SDK event */
|
|
15
|
+
readonly event: "designable-small-tool:xhs:event";
|
|
16
|
+
};
|
|
17
|
+
export type SimulatorJsapiMessageType = (typeof SIMULATOR_JSAPI_MESSAGE_TYPES)[keyof typeof SIMULATOR_JSAPI_MESSAGE_TYPES];
|
|
18
|
+
export interface SimulatorJsapiInvokeMessage {
|
|
19
|
+
type: typeof SIMULATOR_JSAPI_MESSAGE_TYPES.invoke;
|
|
20
|
+
raw: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SimulatorJsapiInvokeCallbackMessage {
|
|
23
|
+
type: typeof SIMULATOR_JSAPI_MESSAGE_TYPES.invokeCallback;
|
|
24
|
+
callbackId: string;
|
|
25
|
+
result: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface SimulatorJsapiEventMessage {
|
|
28
|
+
type: typeof SIMULATOR_JSAPI_MESSAGE_TYPES.event;
|
|
29
|
+
payload: unknown;
|
|
30
|
+
}
|
|
31
|
+
export interface SimulatorJsapiReadyMessage {
|
|
32
|
+
type: typeof SIMULATOR_JSAPI_MESSAGE_TYPES.ready;
|
|
33
|
+
agent: "designable-small-tool-simulator";
|
|
34
|
+
timestamp: number;
|
|
35
|
+
}
|
|
36
|
+
export type SimulatorJsapiMessage = SimulatorJsapiReadyMessage | SimulatorJsapiInvokeMessage | SimulatorJsapiInvokeCallbackMessage | SimulatorJsapiEventMessage;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport —— 字节搬运通道接口(介质相关)
|
|
3
|
+
*
|
|
4
|
+
* 本接口由 Renderer 实现:
|
|
5
|
+
* - IframeTransport:postMessage / window.message
|
|
6
|
+
* - WebviewTransport:webview.send / ipc-message
|
|
7
|
+
*
|
|
8
|
+
* 业务层(BridgeProtocol)只面向本接口编程,不感知介质差异。
|
|
9
|
+
*
|
|
10
|
+
* 设计意图见:design/mini-app/mini-app-simulator-refactor-design.md §6
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* 通道载荷格式:仅约定一个 `type` 字符串作为路由依据,其它字段透传。
|
|
14
|
+
* BridgeProtocol 在其之上叠加 `{ raw, callbackId, result, payload }` 等语义字段。
|
|
15
|
+
*/
|
|
16
|
+
export interface TransportMessage {
|
|
17
|
+
type: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Renderer 持有 / Kernel 注入给 BridgeProtocol 的统一通道。
|
|
22
|
+
*/
|
|
23
|
+
export interface Transport {
|
|
24
|
+
/**
|
|
25
|
+
* mount + 子文档/子进程通道就绪后 resolve。
|
|
26
|
+
* - iframe:`iframe.onload` 后
|
|
27
|
+
* - webview:`dom-ready` 后
|
|
28
|
+
*
|
|
29
|
+
* 在 ready 之前 `post(...)` 也允许调用,但实现可选择缓冲到 ready 后发出。
|
|
30
|
+
*/
|
|
31
|
+
readonly ready: Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 将 Transport 绑定到目标 DOM 元素(iframe / webview)。
|
|
34
|
+
* 由 Renderer 在 mount 阶段调用,Kernel 创建 Transport 后注入。
|
|
35
|
+
*/
|
|
36
|
+
attach(element: HTMLElement): void;
|
|
37
|
+
/**
|
|
38
|
+
* 通知 Transport 通道已就绪(iframe.onload / webview dom-ready)。
|
|
39
|
+
* 重复调用幂等。
|
|
40
|
+
*/
|
|
41
|
+
markReady(): void;
|
|
42
|
+
/** 宿主 → 小应用 */
|
|
43
|
+
post(message: TransportMessage): void;
|
|
44
|
+
/** 小应用 → 宿主;返回 unsubscribe */
|
|
45
|
+
onMessage(handler: (message: TransportMessage) => void): () => void;
|
|
46
|
+
/** 卸载所有监听,幂等。 */
|
|
47
|
+
destroy(): void;
|
|
48
|
+
}
|