@skillbuilder/miniapp-simulator 0.0.0-beta-20261023084421 → 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 +52 -2
- package/dist/sw/miniapp-simulator-service-worker.js +487 -371
- package/dist/sw/miniapp-simulator-service-worker.min.js +1 -1
- package/docs/external-cli-workbench.md +5 -7
- package/docs/npm-integration-verification.md +1 -11
- package/docs/remote-architecture.md +3 -2
- package/docs/worker-runtime.md +62 -0
- package/lib/{client-BZtD2VtP.mjs → client-CO-UjIY3.mjs} +141 -138
- package/lib/index-B5oixYXr.mjs +574 -0
- package/lib/index.js +1152 -1075
- package/lib/remote-client.js +2 -2
- package/lib/types/capabilities/constants.d.ts +1 -0
- package/lib/types/capabilities/csp-snippet.d.ts +2 -0
- package/lib/types/capabilities/security-head-snippet.d.ts +3 -2
- package/lib/types/index.d.ts +8 -1
- package/lib/types/injection/types.d.ts +2 -0
- package/lib/types/remote/protocol.d.ts +7 -2
- package/lib/types/renderer/content-host/iframe-content-host.d.ts +1 -0
- package/lib/types/sw/protocol.d.ts +3 -1
- package/lib/types/sw/served-entry.d.ts +1 -1
- package/lib/types/sw/service-worker-host.d.ts +1 -0
- package/lib/types/sw/worker/globals.d.ts +1 -0
- package/lib/types/sw/worker/mount-store.d.ts +5 -0
- package/lib/types/types/options.d.ts +8 -1
- package/lib/types/workbench/server/index.d.ts +1 -1
- 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 +1 -1
- package/lib/workbench.js +1 -1
- package/package.json +1 -1
- package/lib/index-BW85h7kT.mjs +0 -505
package/README.md
CHANGED
|
@@ -45,6 +45,57 @@ import {
|
|
|
45
45
|
} from "@skillbuilder/miniapp-simulator";
|
|
46
46
|
```
|
|
47
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
|
+
|
|
48
99
|
## 快速接入
|
|
49
100
|
|
|
50
101
|
```ts
|
|
@@ -146,7 +197,7 @@ const workbench = createMiniAppWorkbench({
|
|
|
146
197
|
projectName: "demo-minitool",
|
|
147
198
|
viewport: { width: 393, height: 852 },
|
|
148
199
|
remote: {
|
|
149
|
-
serviceUrl: "https://miniapp-sandbox.
|
|
200
|
+
serviceUrl: "https://miniapp-sandbox.xiaohongshu.com/simulator/",
|
|
150
201
|
appId: "demo-minitool",
|
|
151
202
|
businessLine: BusinessLine.skillDesignable,
|
|
152
203
|
},
|
|
@@ -193,7 +244,6 @@ pnpm --filter @skillbuilder/miniapp-simulator mock:cli-dev -- \
|
|
|
193
244
|
外部 CLI 只需直接安装本包:`--dir` 对应 Node 入口的 `rootDir`,`--no-open` 由 CLI 自己控制打开浏览器,`-h` / `--help` 输出帮助。
|
|
194
245
|
设备、场景、入口识别和文件监听由工作台处理;完整参数与职责见 [`external-cli-workbench.md`](./docs/external-cli-workbench.md)。
|
|
195
246
|
|
|
196
|
-
发布前运行 `pnpm --filter @skillbuilder/miniapp-simulator test:package`,会真实打包并在仓库外从 tarball 安装,验证导出、类型和本地服务。
|
|
197
247
|
`prepack` 自动执行本地构建;不执行上传或发布。Runtime / SW 仍从远程服务动态加载,外网可达性需单独验收。
|
|
198
248
|
当前验证范围及外发门禁见 [`npm-integration-verification.md`](./docs/npm-integration-verification.md)。
|
|
199
249
|
|