@cfp-studio/iframe 18.4.6 → 18.4.7
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/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type { LogLevel } from "../../src";
|
|
3
3
|
// @ts-ignore
|
|
4
4
|
import inspect from "./object-inspect";
|
|
5
|
-
import {
|
|
5
|
+
import { publishToHost } from "./runtime-bridge";
|
|
6
6
|
|
|
7
7
|
// 注意: 请与 vite 保持同步:
|
|
8
8
|
// https://github.com/vitejs/vite/blob/2de425d0288bfae345c5ced5c84cf67ffccaef48/packages/vite/src/client/client.ts#L117
|
|
@@ -158,6 +158,16 @@ function formatValue(value: any) {
|
|
|
158
158
|
*
|
|
159
159
|
* 不追求精确(不需要漏桶/令牌桶):排查时要的是「知道有过量日志 + 看到样本」,
|
|
160
160
|
* 不是一份完整的日志归档。
|
|
161
|
+
*
|
|
162
|
+
* ## `source` 为什么必须显式给
|
|
163
|
+
*
|
|
164
|
+
* 宿主过去按**级别**分频道(`log`/`info` → Preview Console,`warn`/`error` → 运行时频道),
|
|
165
|
+
* 但级别不等于说话者:React 把**所有**开发期警告都打到 `console.error`,于是
|
|
166
|
+
* 「Function components cannot be given refs」「Each child in a list should have a unique key」
|
|
167
|
+
* 这类**用户代码的输出**全被当成了我們自己的运行时告警。
|
|
168
|
+
*
|
|
169
|
+
* 反过来,本文件的限流汇总用 `warn` 发出,却是**我们自己的话**。两边都按级别推断会同时猜错,
|
|
170
|
+
* 所以说话者只能由生产方标注。
|
|
161
171
|
*/
|
|
162
172
|
const LOG_BURST_WINDOW_MS = 1_000;
|
|
163
173
|
/** 一个窗口内最多转发多少条。超出部分计数后丢弃。 */
|
|
@@ -173,10 +183,12 @@ function publishLogToHost(level: LogLevel, message: string): void {
|
|
|
173
183
|
if (now - burstWindowStart > LOG_BURST_WINDOW_MS) {
|
|
174
184
|
// 上一个窗口结束时若有丢弃,补一条汇总(用 warn,确保宿主默认看得见)。
|
|
175
185
|
if (droppedInWindow > 0) {
|
|
176
|
-
|
|
186
|
+
publishToHost({
|
|
177
187
|
kind: "cfp-log",
|
|
178
188
|
level: "warn",
|
|
179
189
|
message: `[预览页日志过多] 上一秒另有 ${droppedInWindow} 条被丢弃(上限 ${LOG_BURST_LIMIT}/秒)`,
|
|
190
|
+
// 这条是**我们自己**的限流提示,不是用户代码打的 —— 宿主据此把它留在运行时频道。
|
|
191
|
+
source: "runtime",
|
|
180
192
|
timestamp: now,
|
|
181
193
|
});
|
|
182
194
|
}
|
|
@@ -189,7 +201,10 @@ function publishLogToHost(level: LogLevel, message: string): void {
|
|
|
189
201
|
return;
|
|
190
202
|
}
|
|
191
203
|
burstCount += 1;
|
|
192
|
-
|
|
204
|
+
// 用 `publishToHost` 而不是 `postToParent`:后者在 **browser 模式**下
|
|
205
|
+
// (预览页是顶层窗口,`window.parent === window`)直接 return 0,
|
|
206
|
+
// 日志会静默丢失 —— 而组件 console 恰恰是最需要传出去的东西。
|
|
207
|
+
publishToHost({ kind: "cfp-log", level, message, source: "user", timestamp: now });
|
|
193
208
|
} catch {
|
|
194
209
|
// 上报失败绝不能影响页面本身 —— console 拦截已经改了全局行为,这里必须静默。
|
|
195
210
|
}
|
|
@@ -581,11 +581,19 @@ export function publishRuntimeSnapshot(): void {
|
|
|
581
581
|
}
|
|
582
582
|
|
|
583
583
|
/**
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
584
|
+
* 把 `cfp-*` 消息上送宿主。
|
|
585
|
+
*
|
|
586
|
+
* ## 两条子路,两种预览模式各走一条
|
|
587
|
+
*
|
|
588
|
+
* - **browser**:预览页是顶层窗口(`window.parent === window`),`postMessage` 根本无处可发;
|
|
589
|
+
* 必须走 CDP `Runtime.addBinding` 注入的 `__cfpBridge`。
|
|
590
|
+
* - **embedded**:预览页嵌在 `@cfp-studio/app` 里,走 `parent.postMessage`,
|
|
591
|
+
* 由中间层原样转发给 webview。
|
|
592
|
+
*
|
|
593
|
+
* 这就是它存在的原因:任何想要「两种模式都能上送」的消息都必须用它,
|
|
594
|
+
* 而不是直接调 `postToParent` —— 后者在 browser 模式下会静默地什么都不做。
|
|
587
595
|
*/
|
|
588
|
-
function publishToHost(message: unknown): void {
|
|
596
|
+
export function publishToHost(message: unknown): void {
|
|
589
597
|
if (typeof window.__cfpBridge === "function") {
|
|
590
598
|
try {
|
|
591
599
|
window.__cfpBridge(JSON.stringify(message));
|