@cfp-studio/iframe 18.4.0 → 18.4.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.
@@ -6,122 +6,122 @@ import inspect from "./object-inspect";
6
6
  // 注意: 请与 vite 保持同步:
7
7
  // https://github.com/vitejs/vite/blob/2de425d0288bfae345c5ced5c84cf67ffccaef48/packages/vite/src/client/client.ts#L117
8
8
  const HMR_FAILED_UPDATE_REGEX =
9
- /^\[hmr\] Failed to reload (.+)\. This could be due to syntax errors or importing non-existent modules\. \(see errors above\)$/;
9
+ /^\[hmr\] Failed to reload (.+)\. This could be due to syntax errors or importing non-existent modules\. \(see errors above\)$/;
10
10
 
11
11
  /**
12
12
  * 设置日志拦截器。
13
13
  */
14
14
  export function setUpLogInterception() {
15
- const makeLogger =
16
- (level: LogLevel, defaultFn: typeof console.log) =>
17
- (...args: any[]) => {
18
- // Prevent an infinite call stack size if anything here logs.
19
- const self = console[level];
20
- console[level] = defaultFn;
21
- defaultFn(...args);
22
- try {
23
- const firstArg = args[0];
24
- if (typeof firstArg === "string") {
25
- if (firstArg.startsWith("[vite]")) {
26
- // Silence.
27
- return;
28
- }
29
- const hmrMatch = firstArg.match(HMR_FAILED_UPDATE_REGEX);
30
- if (hmrMatch) {
31
- return;
32
- }
33
- }
34
- window.__PREVIEWJS_IFRAME__.reportEvent({
35
- kind: "log-message",
36
- level,
37
- message: formatLogMessage(...args),
38
- timestamp: Date.now(),
39
- });
40
- } finally {
41
- console[level] = self;
42
- }
43
- };
44
- console.log = makeLogger("log", console.log);
45
- console.info = makeLogger("info", console.info);
46
- console.warn = makeLogger("warn", console.warn);
47
- console.error = makeLogger("error", console.error);
48
- window.onerror = (message, source, lineno, colno, error) => {
49
- window.__PREVIEWJS_IFRAME__.reportEvent({
50
- kind: "error",
51
- source: "renderer",
52
- message: formatError(error, message),
53
- });
54
- };
55
- window.onunhandledrejection = (event) => {
56
- const message = formatError(event.reason);
57
- window.__PREVIEWJS_IFRAME__.reportEvent({
58
- kind: "error",
59
- source:
60
- message.includes("Failed to fetch dynamically imported module") ||
61
- message.includes("Failed to reload")
62
- ? "vite"
63
- : "renderer",
64
- message,
65
- });
66
- };
15
+ const makeLogger =
16
+ (level: LogLevel, defaultFn: typeof console.log) =>
17
+ (...args: any[]) => {
18
+ // Prevent an infinite call stack size if anything here logs.
19
+ const self = console[level];
20
+ console[level] = defaultFn;
21
+ defaultFn(...args);
22
+ try {
23
+ const firstArg = args[0];
24
+ if (typeof firstArg === "string") {
25
+ if (firstArg.startsWith("[vite]")) {
26
+ // Silence.
27
+ return;
28
+ }
29
+ const hmrMatch = firstArg.match(HMR_FAILED_UPDATE_REGEX);
30
+ if (hmrMatch) {
31
+ return;
32
+ }
33
+ }
34
+ window.__PREVIEWJS_IFRAME__.reportEvent({
35
+ kind: "log-message",
36
+ level,
37
+ message: formatLogMessage(...args),
38
+ timestamp: Date.now(),
39
+ });
40
+ } finally {
41
+ console[level] = self;
42
+ }
43
+ };
44
+ console.log = makeLogger("log", console.log);
45
+ console.info = makeLogger("info", console.info);
46
+ console.warn = makeLogger("warn", console.warn);
47
+ console.error = makeLogger("error", console.error);
48
+ window.onerror = (message, source, lineno, colno, error) => {
49
+ window.__PREVIEWJS_IFRAME__.reportEvent({
50
+ kind: "error",
51
+ source: "renderer",
52
+ message: formatError(error, message),
53
+ });
54
+ };
55
+ window.onunhandledrejection = (event) => {
56
+ const message = formatError(event.reason);
57
+ window.__PREVIEWJS_IFRAME__.reportEvent({
58
+ kind: "error",
59
+ source:
60
+ message.includes("Failed to fetch dynamically imported module") || message.includes("Failed to reload")
61
+ ? "vite"
62
+ : "renderer",
63
+ message,
64
+ });
65
+ };
67
66
  }
68
67
 
69
68
  /**
70
69
  * 格式化错误信息。
71
- * @param error
72
- * @param message
73
- * @returns
70
+ * @param error
71
+ * @param message
72
+ * @returns
74
73
  */
75
74
 
76
75
  function formatError(error?: any, message?: any): string {
77
- // 如果 error 对象存在且包含 stack 和 message 属性
78
- if (error?.stack && error.message) {
79
- let stackMessage = error.stack;
80
- // 确保 stack 信息包含错误消息
81
- if (!stackMessage.includes(error.message)) {
82
- stackMessage = `${error.message}\n${stackMessage}`;
83
- }
84
- return stackMessage;
85
- } if (message) {
86
- return `${message}`;
87
- }
88
- return `${error}`;
76
+ // 如果 error 对象存在且包含 stack 和 message 属性
77
+ if (error?.stack && error.message) {
78
+ let stackMessage = error.stack;
79
+ // 确保 stack 信息包含错误消息
80
+ if (!stackMessage.includes(error.message)) {
81
+ stackMessage = `${error.message}\n${stackMessage}`;
82
+ }
83
+ return stackMessage;
84
+ }
85
+ if (message) {
86
+ return `${message}`;
87
+ }
88
+ return `${error}`;
89
89
  }
90
90
 
91
91
  /**
92
92
  * 格式化日志消息。
93
- * @param args
94
- * @returns
93
+ * @param args
94
+ * @returns
95
95
  */
96
96
  function formatLogMessage(...args: any[]) {
97
- if (args.length === 0) {
98
- return "";
99
- }
100
- let message = formatValue(args.shift());
101
- message = message.replace(/%s/g, () => formatValue(args.shift()));
102
- return [message, ...args.map(formatValue)].join(" ");
97
+ if (args.length === 0) {
98
+ return "";
99
+ }
100
+ let message = formatValue(args.shift());
101
+ message = message.replace(/%s/g, () => formatValue(args.shift()));
102
+ return [message, ...args.map(formatValue)].join(" ");
103
103
  }
104
104
 
105
105
  /**
106
106
  * 格式化值。
107
- * @param value
108
- * @returns
107
+ * @param value
108
+ * @returns
109
109
  */
110
110
  function formatValue(value: any) {
111
- if (typeof value === "string") {
112
- return value;
113
- }
114
- const formatted = inspect(value);
115
- if (typeof formatted !== "string") {
116
- // This can happen in rare cases e.g. toString() not actually returning a string.
117
- try {
118
- return JSON.stringify(value);
119
- } catch {
120
- return "[Unable to represent value as string]";
121
- }
122
- }
123
- if (formatted.at(0) === "[" && formatted.at(-1) === "]") {
124
- return formatted.substring(1, formatted.length - 1);
125
- }
126
- return formatted;
111
+ if (typeof value === "string") {
112
+ return value;
113
+ }
114
+ const formatted = inspect(value);
115
+ if (typeof formatted !== "string") {
116
+ // This can happen in rare cases e.g. toString() not actually returning a string.
117
+ try {
118
+ return JSON.stringify(value);
119
+ } catch {
120
+ return "[Unable to represent value as string]";
121
+ }
122
+ }
123
+ if (formatted.at(0) === "[" && formatted.at(-1) === "]") {
124
+ return formatted.substring(1, formatted.length - 1);
125
+ }
126
+ return formatted;
127
127
  }