miaoda-react-devkit 0.1.1-beta.1 → 0.1.1-beta.10
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/cloud/vite.config.js +118 -0
- package/dist/index.cjs +253 -69
- package/dist/index.d.cts +111 -2
- package/dist/index.d.ts +111 -2
- package/dist/index.js +250 -69
- package/dist/stubs/sentry-react-noop-stub.js +78 -0
- package/dist/stubs/sentry-react-stub.js +187 -69
- package/dist/vite/index.cjs +20 -1
- package/dist/vite/index.d.cts +3 -1
- package/dist/vite/index.d.ts +3 -1
- package/dist/vite/index.js +19 -1
- package/package.json +3 -1
- package/README.md +0 -181
- package/cloud/vite.config.ts +0 -156
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// cloud/vite.config.ts
|
|
2
|
+
import * as vite from "vite";
|
|
3
|
+
import { loadConfigFromFile, mergeConfig } from "vite";
|
|
4
|
+
import { withSentryStub, hmrTogglePlugin } from "../dist/vite/index.js";
|
|
5
|
+
import {
|
|
6
|
+
makeTagger,
|
|
7
|
+
injectedGuiListenerPlugin,
|
|
8
|
+
injectOnErrorPlugin,
|
|
9
|
+
monitorPlugin,
|
|
10
|
+
fontsCodePlugin,
|
|
11
|
+
mdClickPlugin,
|
|
12
|
+
extractRouterPlugin,
|
|
13
|
+
patchSupabasePlugins
|
|
14
|
+
} from "miaoda-sc-plugin";
|
|
15
|
+
import path from "path";
|
|
16
|
+
import fs from "fs";
|
|
17
|
+
var projectRoot = process.cwd();
|
|
18
|
+
var appType = process.env.DEVKIT_APP_TYPE ?? "freestyle";
|
|
19
|
+
var cdnHost = process.env.DEVKIT_CDN_HOST ?? "resource-static.cdn.bcebos.com";
|
|
20
|
+
var env = process.env.DEVKIT_ENV ?? "";
|
|
21
|
+
var sentryDsn = process.env.DEVKIT_SENTRY_DSN ?? "";
|
|
22
|
+
var miaodaEnv = process.env.DEVKIT_MIAODA_ENV ?? "";
|
|
23
|
+
var appId = process.env.DEVKIT_APP_ID ?? "";
|
|
24
|
+
var isOnline = env === "online";
|
|
25
|
+
var loaded = await loadConfigFromFile(
|
|
26
|
+
{ command: "serve", mode: "development" },
|
|
27
|
+
path.join(projectRoot, "vite.config.ts"),
|
|
28
|
+
projectRoot
|
|
29
|
+
);
|
|
30
|
+
var projectConfig = loaded?.config ?? {};
|
|
31
|
+
var configPath = path.join(projectRoot, "vite.config.ts");
|
|
32
|
+
if (!loaded) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`[miaoda-devkit] \u627E\u4E0D\u5230\u9879\u76EE vite \u914D\u7F6E\uFF1A${configPath}
|
|
35
|
+
\u8BF7\u786E\u8BA4\u9879\u76EE\u6839\u76EE\u5F55\u4E0B\u5B58\u5728 vite.config.ts`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
var plugins = (projectConfig.plugins ?? []).flat(Infinity).filter((p) => !!p);
|
|
39
|
+
var hasReact = plugins.some((p) => p.name === "vite:react-babel" || p.name === "vite:react-swc");
|
|
40
|
+
if (!hasReact) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`[miaoda-devkit] \u9879\u76EE vite \u914D\u7F6E\u7F3A\u5C11 React \u63D2\u4EF6\u3002
|
|
43
|
+
\u8BF7\u5728 ${configPath} \u7684 plugins \u4E2D\u6DFB\u52A0 react()\uFF08@vitejs/plugin-react \u6216 @vitejs/plugin-react-swc\uFF09`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
var possibleEntries = ["./src/main.tsx", "./src/main.ts", "./src/index.tsx", "./src/index.ts"];
|
|
47
|
+
var warmupFiles = possibleEntries.filter((e) => fs.existsSync(path.join(projectRoot, e)));
|
|
48
|
+
var SENTRY_IMPORT_RE = /from\s+['"]@sentry\/react['"]/;
|
|
49
|
+
function entryHasSentryImport(root, entries) {
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
const fullPath = path.join(root, entry);
|
|
52
|
+
if (!fs.existsSync(fullPath)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
if (SENTRY_IMPORT_RE.test(fs.readFileSync(fullPath, "utf-8"))) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
var hasSentryReact = entryHasSentryImport(projectRoot, possibleEntries);
|
|
65
|
+
var viteVersionInfo = {
|
|
66
|
+
version: vite.version,
|
|
67
|
+
rollupVersion: vite.rollupVersion ?? null,
|
|
68
|
+
rolldownVersion: vite.rolldownVersion ?? null,
|
|
69
|
+
isRolldownVite: "rolldownVersion" in vite
|
|
70
|
+
};
|
|
71
|
+
var injectedGuiUrl = `https://${cdnHost}/common/v2/${isOnline ? "" : `${env}/`}injected.js`;
|
|
72
|
+
var cloudPlugins = [
|
|
73
|
+
...hasSentryReact ? [withSentryStub()] : [monitorPlugin({
|
|
74
|
+
scriptSrc: `https://${cdnHost}/sentry/browser.sentry.min.js`,
|
|
75
|
+
sentryDsn,
|
|
76
|
+
environment: miaodaEnv,
|
|
77
|
+
appId
|
|
78
|
+
})],
|
|
79
|
+
// web 类型需要 DOM 节点打标(GUI使用)
|
|
80
|
+
...appType === "web" ? [
|
|
81
|
+
makeTagger(),
|
|
82
|
+
mdClickPlugin({ appType }),
|
|
83
|
+
fontsCodePlugin(),
|
|
84
|
+
injectedGuiListenerPlugin({ path: injectedGuiUrl })
|
|
85
|
+
] : [],
|
|
86
|
+
injectedGuiListenerPlugin({ path: injectedGuiUrl }),
|
|
87
|
+
injectOnErrorPlugin(),
|
|
88
|
+
hmrTogglePlugin(),
|
|
89
|
+
extractRouterPlugin(),
|
|
90
|
+
patchSupabasePlugins()
|
|
91
|
+
];
|
|
92
|
+
var cloudConfig = {
|
|
93
|
+
define: {
|
|
94
|
+
__VITE_INFO__: JSON.stringify(viteVersionInfo)
|
|
95
|
+
},
|
|
96
|
+
// 将缓存目录固定到项目本地,避免在 /workspace/node_modules/ 下创建
|
|
97
|
+
cacheDir: path.resolve(projectRoot, "node_modules/.vite"),
|
|
98
|
+
server: {
|
|
99
|
+
warmup: warmupFiles.length > 0 ? { clientFiles: warmupFiles } : {}
|
|
100
|
+
},
|
|
101
|
+
plugins: cloudPlugins
|
|
102
|
+
};
|
|
103
|
+
function dedupePlugins(config) {
|
|
104
|
+
if (!Array.isArray(config.plugins)) return config;
|
|
105
|
+
const flattened = config.plugins.flat(Infinity);
|
|
106
|
+
const seen = /* @__PURE__ */ new Set();
|
|
107
|
+
const deduped = flattened.reverse().filter((p) => {
|
|
108
|
+
if (!p || !p.name) return true;
|
|
109
|
+
if (seen.has(p.name)) return false;
|
|
110
|
+
seen.add(p.name);
|
|
111
|
+
return true;
|
|
112
|
+
}).reverse();
|
|
113
|
+
return { ...config, plugins: deduped };
|
|
114
|
+
}
|
|
115
|
+
var vite_config_default = dedupePlugins(mergeConfig(projectConfig, cloudConfig));
|
|
116
|
+
export {
|
|
117
|
+
vite_config_default as default
|
|
118
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -22,24 +22,22 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
22
22
|
// src/index.ts
|
|
23
23
|
var src_exports = {};
|
|
24
24
|
__export(src_exports, {
|
|
25
|
+
DEFAULT_AUTH_URL_PATTERNS: () => DEFAULT_AUTH_URL_PATTERNS,
|
|
26
|
+
DEFAULT_AUTH_WHITELIST_CODES: () => DEFAULT_AUTH_WHITELIST_CODES,
|
|
25
27
|
DEFAULT_CONSOLE_FILTER_KEYWORDS: () => DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
26
28
|
DEFAULT_IGNORE_URLS: () => DEFAULT_IGNORE_URLS,
|
|
27
29
|
DEFAULT_PGREST_URL_PATTERNS: () => DEFAULT_PGREST_URL_PATTERNS,
|
|
28
30
|
DEFAULT_PGREST_WHITELIST_CODES: () => DEFAULT_PGREST_WHITELIST_CODES,
|
|
29
31
|
SentryCapture: () => SentryCapture,
|
|
30
32
|
createNoopSubscriber: () => createNoopSubscriber,
|
|
31
|
-
createPostMessageSubscriber: () => createPostMessageSubscriber
|
|
33
|
+
createPostMessageSubscriber: () => createPostMessageSubscriber,
|
|
34
|
+
createSentryEventSubscriber: () => createSentryEventSubscriber
|
|
32
35
|
});
|
|
33
36
|
module.exports = __toCommonJS(src_exports);
|
|
34
37
|
|
|
35
38
|
// src/capture/console.ts
|
|
36
39
|
var import_core = require("@sentry/core");
|
|
37
|
-
|
|
38
|
-
"Sentry initialized successfully",
|
|
39
|
-
"[vite] ",
|
|
40
|
-
"Download the React DevTools for a better development experience"
|
|
41
|
-
];
|
|
42
|
-
function serializeArg(arg) {
|
|
40
|
+
function formatConsoleArg(arg) {
|
|
43
41
|
if (typeof arg === "string") return arg;
|
|
44
42
|
if (arg instanceof Error) return arg.message;
|
|
45
43
|
try {
|
|
@@ -48,9 +46,17 @@ function serializeArg(arg) {
|
|
|
48
46
|
return String(arg);
|
|
49
47
|
}
|
|
50
48
|
}
|
|
49
|
+
function formatConsoleArgs(args) {
|
|
50
|
+
return args.map(formatConsoleArg).join(" ");
|
|
51
|
+
}
|
|
52
|
+
var DEFAULT_CONSOLE_FILTER_KEYWORDS = [
|
|
53
|
+
"Sentry initialized successfully",
|
|
54
|
+
"[vite] ",
|
|
55
|
+
"Download the React DevTools for a better development experience"
|
|
56
|
+
];
|
|
51
57
|
function shouldFilterConsole(args, keywords) {
|
|
52
58
|
if (keywords.length === 0) return false;
|
|
53
|
-
const text = args
|
|
59
|
+
const text = formatConsoleArgs(args);
|
|
54
60
|
return keywords.some((kw) => text.includes(kw));
|
|
55
61
|
}
|
|
56
62
|
function shouldFilterConsoleMessage(message, keywords) {
|
|
@@ -66,9 +72,20 @@ function createConsoleObserver(keywords, subscribers) {
|
|
|
66
72
|
for (const sub of subscribers) sub.onConsole(info);
|
|
67
73
|
});
|
|
68
74
|
},
|
|
69
|
-
beforeBreadcrumb(breadcrumb,
|
|
75
|
+
beforeBreadcrumb(breadcrumb, hint) {
|
|
70
76
|
if (breadcrumb.category === "console" && breadcrumb.message != null) {
|
|
71
77
|
if (shouldFilterConsoleMessage(breadcrumb.message, keywords)) return null;
|
|
78
|
+
const h = hint;
|
|
79
|
+
const consoleType = h?.level ?? "log";
|
|
80
|
+
const stacks = (h?.input ?? []).filter((arg) => arg instanceof Error && arg.stack != null).map((err) => err.stack);
|
|
81
|
+
const originalMessage = breadcrumb.message;
|
|
82
|
+
breadcrumb.message = `[${consoleType}]${originalMessage}`;
|
|
83
|
+
breadcrumb.data = {
|
|
84
|
+
...breadcrumb.data ?? {},
|
|
85
|
+
consoleType,
|
|
86
|
+
message: originalMessage,
|
|
87
|
+
...stacks.length > 0 ? { stack: stacks.join("\n\n") } : {}
|
|
88
|
+
};
|
|
72
89
|
}
|
|
73
90
|
return breadcrumb;
|
|
74
91
|
}
|
|
@@ -114,9 +131,42 @@ function getFetchRequestArgBody(fetchArgs = []) {
|
|
|
114
131
|
return fetchArgs[1].body;
|
|
115
132
|
}
|
|
116
133
|
|
|
134
|
+
// src/utils.ts
|
|
135
|
+
function stripUrlOrigin(url) {
|
|
136
|
+
try {
|
|
137
|
+
const u = new URL(url);
|
|
138
|
+
return u.pathname + u.search + u.hash;
|
|
139
|
+
} catch {
|
|
140
|
+
return url;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
117
144
|
// src/capture/pgrest.ts
|
|
118
145
|
var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
|
|
119
|
-
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
146
|
+
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
147
|
+
"22P02",
|
|
148
|
+
// 无效的文本表示(如类型转换失败)
|
|
149
|
+
"42703",
|
|
150
|
+
// 未定义的列
|
|
151
|
+
"42P01",
|
|
152
|
+
// 未定义的表
|
|
153
|
+
"42501",
|
|
154
|
+
// 权限不足
|
|
155
|
+
"23503",
|
|
156
|
+
// 外键约束失败
|
|
157
|
+
"42P17",
|
|
158
|
+
// 对象已存在
|
|
159
|
+
"22008",
|
|
160
|
+
// 日期时间字段值超出范围
|
|
161
|
+
"23505",
|
|
162
|
+
// 唯一键冲突
|
|
163
|
+
"PGRST116",
|
|
164
|
+
// 期望单行但返回多行
|
|
165
|
+
"PGRST200",
|
|
166
|
+
// 外键关系查找失败
|
|
167
|
+
"PGRST204"
|
|
168
|
+
// 响应中找不到列
|
|
169
|
+
];
|
|
120
170
|
function isPgRestRequest(url, patterns) {
|
|
121
171
|
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
122
172
|
}
|
|
@@ -141,8 +191,38 @@ function parsePgRestErrorBody(body) {
|
|
|
141
191
|
};
|
|
142
192
|
}
|
|
143
193
|
|
|
194
|
+
// src/capture/auth.ts
|
|
195
|
+
var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
|
|
196
|
+
var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
|
|
197
|
+
function isAuthRequest(url, patterns) {
|
|
198
|
+
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
199
|
+
}
|
|
200
|
+
function parseAuthErrorBody(body) {
|
|
201
|
+
if (!body) return null;
|
|
202
|
+
let parsed;
|
|
203
|
+
try {
|
|
204
|
+
parsed = JSON.parse(body);
|
|
205
|
+
} catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
if (typeof parsed !== "object") return null;
|
|
209
|
+
if (parsed === null) return null;
|
|
210
|
+
const p = parsed;
|
|
211
|
+
if (typeof p["code"] !== "string") return null;
|
|
212
|
+
if (typeof p["message"] !== "string") return null;
|
|
213
|
+
return {
|
|
214
|
+
code: p["code"],
|
|
215
|
+
message: p["message"],
|
|
216
|
+
requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
144
220
|
// src/capture/network.ts
|
|
145
221
|
var BODY_TRUNCATE_LIMIT = 2e3;
|
|
222
|
+
function calcDuration(hint) {
|
|
223
|
+
if (hint?.startTimestamp == null || hint?.endTimestamp == null) return void 0;
|
|
224
|
+
return Math.round((hint.endTimestamp - hint.startTimestamp) * 1e3);
|
|
225
|
+
}
|
|
146
226
|
var DEFAULT_IGNORE_URLS = [
|
|
147
227
|
/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot|map)(\?.*)?$/,
|
|
148
228
|
/\.chunk\./,
|
|
@@ -192,7 +272,15 @@ function handleXhrBreadcrumb(breadcrumb, hint, handler) {
|
|
|
192
272
|
breadcrumb.data["requestBody"] = requestBody;
|
|
193
273
|
breadcrumb.data["responseBody"] = responseBody;
|
|
194
274
|
}
|
|
195
|
-
|
|
275
|
+
const duration = calcDuration(hint);
|
|
276
|
+
const relativeUrl = stripUrlOrigin(url);
|
|
277
|
+
const statusStr = statusCode != null ? String(statusCode) : "failed";
|
|
278
|
+
const durationStr = duration != null ? ` (${duration}ms)` : "";
|
|
279
|
+
breadcrumb.message = `XHR ${method} ${relativeUrl} -> ${statusStr}${durationStr}`;
|
|
280
|
+
if (breadcrumb.data) {
|
|
281
|
+
breadcrumb.data["duration"] = duration;
|
|
282
|
+
}
|
|
283
|
+
void handler({ type: "xhr", method, url, statusCode, requestBody, responseBody, isError, duration });
|
|
196
284
|
}
|
|
197
285
|
async function handleFetchBreadcrumb(breadcrumb, hint, handler) {
|
|
198
286
|
const data = breadcrumb.data;
|
|
@@ -202,19 +290,24 @@ async function handleFetchBreadcrumb(breadcrumb, hint, handler) {
|
|
|
202
290
|
const isError = hint?.data != null;
|
|
203
291
|
const response = hint?.response;
|
|
204
292
|
const cause = hint?.data instanceof Error ? hint.data : void 0;
|
|
205
|
-
const
|
|
206
|
-
const reqBody = getFetchRequestArgBody(fetchArgs ?? []);
|
|
293
|
+
const reqBody = getFetchRequestArgBody(hint?.input ?? []);
|
|
207
294
|
const [rawRequestBody] = getBodyString(reqBody);
|
|
208
295
|
const requestBody = rawRequestBody != null ? truncateBody(rawRequestBody) : void 0;
|
|
296
|
+
const duration = calcDuration(hint);
|
|
297
|
+
const relativeUrl = stripUrlOrigin(url);
|
|
298
|
+
const statusStr = statusCode != null ? String(statusCode) : "failed";
|
|
299
|
+
const durationStr = duration != null ? ` (${duration}ms)` : "";
|
|
300
|
+
breadcrumb.message = `Fetch ${method} ${relativeUrl} -> ${statusStr}${durationStr}`;
|
|
209
301
|
if (breadcrumb.data) {
|
|
210
302
|
breadcrumb.data["requestBody"] = requestBody;
|
|
303
|
+
breadcrumb.data["duration"] = duration;
|
|
211
304
|
}
|
|
212
305
|
const contentType = response?.headers?.get("content-type") ?? "";
|
|
213
306
|
if (isStreamByContentType(contentType)) {
|
|
214
307
|
if (breadcrumb.data) {
|
|
215
308
|
breadcrumb.data["responseBody"] = "[streaming response - not captured]";
|
|
216
309
|
}
|
|
217
|
-
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody: "[streaming response - not captured]", isError, cause });
|
|
310
|
+
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody: "[streaming response - not captured]", isError, cause, duration });
|
|
218
311
|
return;
|
|
219
312
|
}
|
|
220
313
|
let responseBody;
|
|
@@ -234,18 +327,23 @@ async function handleFetchBreadcrumb(breadcrumb, hint, handler) {
|
|
|
234
327
|
if (breadcrumb.data) {
|
|
235
328
|
breadcrumb.data["responseBody"] = responseBody;
|
|
236
329
|
}
|
|
237
|
-
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody, isError, cause });
|
|
330
|
+
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody, isError, cause, duration });
|
|
238
331
|
}
|
|
239
332
|
function createNetworkObserver(opts, subscribers) {
|
|
240
333
|
const shouldIgnore = createUrlMatcher(opts.ignoreUrls);
|
|
241
334
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
242
335
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
336
|
+
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
337
|
+
const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
|
|
338
|
+
const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
|
|
243
339
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
244
340
|
if (statusCode == null) return;
|
|
245
341
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
246
342
|
const pgRestError = parsePgRestErrorBody(responseBody);
|
|
247
343
|
if (!pgRestError) return;
|
|
248
344
|
if (pgRestWhitelistCodes.length > 0 && !pgRestWhitelistCodes.includes(pgRestError.code)) return;
|
|
345
|
+
const allBreadcrumbs = (0, import_core3.getIsolationScope)().getScopeData().breadcrumbs;
|
|
346
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
249
347
|
const info = {
|
|
250
348
|
type: requestType,
|
|
251
349
|
method,
|
|
@@ -253,10 +351,31 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
253
351
|
statusCode,
|
|
254
352
|
pgRestError,
|
|
255
353
|
requestBody,
|
|
256
|
-
responseBody
|
|
354
|
+
responseBody,
|
|
355
|
+
precedingBreadcrumbs
|
|
257
356
|
};
|
|
258
357
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
259
358
|
}
|
|
359
|
+
function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
360
|
+
if (statusCode == null) return;
|
|
361
|
+
if (!isAuthRequest(url, authPatterns)) return;
|
|
362
|
+
const authError = parseAuthErrorBody(responseBody);
|
|
363
|
+
if (!authError) return;
|
|
364
|
+
if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
|
|
365
|
+
const allBreadcrumbs = (0, import_core3.getIsolationScope)().getScopeData().breadcrumbs;
|
|
366
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
367
|
+
const info = {
|
|
368
|
+
type: requestType,
|
|
369
|
+
method,
|
|
370
|
+
url,
|
|
371
|
+
statusCode,
|
|
372
|
+
authError,
|
|
373
|
+
requestBody,
|
|
374
|
+
responseBody,
|
|
375
|
+
precedingBreadcrumbs
|
|
376
|
+
};
|
|
377
|
+
for (const sub of subscribers) sub.onAuthWhitelistError(info);
|
|
378
|
+
}
|
|
260
379
|
function makeHandler(requestType) {
|
|
261
380
|
return (info) => {
|
|
262
381
|
for (const sub of subscribers) void sub.onNetworkRequest(info);
|
|
@@ -268,6 +387,14 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
268
387
|
info.requestBody,
|
|
269
388
|
info.responseBody
|
|
270
389
|
);
|
|
390
|
+
tryDispatchAuthWhitelistError(
|
|
391
|
+
requestType,
|
|
392
|
+
info.method,
|
|
393
|
+
info.url,
|
|
394
|
+
info.statusCode,
|
|
395
|
+
info.requestBody,
|
|
396
|
+
info.responseBody
|
|
397
|
+
);
|
|
271
398
|
};
|
|
272
399
|
}
|
|
273
400
|
return {
|
|
@@ -280,10 +407,11 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
280
407
|
const fetchHandler = makeHandler("fetch");
|
|
281
408
|
client.on("beforeAddBreadcrumb", (breadcrumb, hint) => {
|
|
282
409
|
const url = breadcrumb.data?.url ?? "";
|
|
283
|
-
if (shouldIgnore(url)) return;
|
|
284
410
|
if (breadcrumb.category === "xhr") {
|
|
411
|
+
if (shouldIgnore(url)) return;
|
|
285
412
|
handleXhrBreadcrumb(breadcrumb, hint, xhrHandler);
|
|
286
413
|
} else if (breadcrumb.category === "fetch") {
|
|
414
|
+
if (shouldIgnore(url)) return;
|
|
287
415
|
void handleFetchBreadcrumb(breadcrumb, hint, fetchHandler);
|
|
288
416
|
}
|
|
289
417
|
});
|
|
@@ -376,56 +504,10 @@ var PostMessageSubscriber = class {
|
|
|
376
504
|
};
|
|
377
505
|
this.send(msg, "*");
|
|
378
506
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
if (!isError && !isClientError) return;
|
|
382
|
-
const urlPath = extractUrlPath(url);
|
|
383
|
-
if (isError) {
|
|
384
|
-
const causeDesc = cause != null ? String(cause) : "NetworkError";
|
|
385
|
-
const errorMessage = `${method} ${urlPath} failed: ${causeDesc}`;
|
|
386
|
-
const msg = {
|
|
387
|
-
type: "GLOBAL_ERROR",
|
|
388
|
-
message: errorMessage,
|
|
389
|
-
error: cause != null ? { name: cause.name, message: cause.message, stack: cause.stack } : null,
|
|
390
|
-
errorData: {
|
|
391
|
-
name: cause?.name ?? "NetworkError",
|
|
392
|
-
message: cause?.message ?? causeDesc,
|
|
393
|
-
stack: cause?.stack ?? null,
|
|
394
|
-
fileName: url,
|
|
395
|
-
lineNumber: 0,
|
|
396
|
-
columnNumber: 0,
|
|
397
|
-
httpPath: urlPath,
|
|
398
|
-
httpStatus: 0,
|
|
399
|
-
httpMethod: method,
|
|
400
|
-
requestBody: requestBody ?? "",
|
|
401
|
-
responseBody: ""
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
this.send(msg, "*");
|
|
405
|
-
} else {
|
|
406
|
-
const errorMessage = `${method} ${urlPath} -> ${statusCode}`;
|
|
407
|
-
const msg = {
|
|
408
|
-
type: "GLOBAL_ERROR",
|
|
409
|
-
message: errorMessage,
|
|
410
|
-
error: null,
|
|
411
|
-
errorData: {
|
|
412
|
-
name: "HTTPClientError",
|
|
413
|
-
message: errorMessage,
|
|
414
|
-
stack: null,
|
|
415
|
-
fileName: url,
|
|
416
|
-
lineNumber: 0,
|
|
417
|
-
columnNumber: 0,
|
|
418
|
-
httpPath: urlPath,
|
|
419
|
-
httpStatus: statusCode,
|
|
420
|
-
httpMethod: method,
|
|
421
|
-
requestBody: requestBody ?? "",
|
|
422
|
-
responseBody: responseBody ?? ""
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
this.send(msg, "*");
|
|
426
|
-
}
|
|
507
|
+
// postMessage 场景不需要上报 network 日志
|
|
508
|
+
onNetworkRequest(_info) {
|
|
427
509
|
}
|
|
428
|
-
onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody }) {
|
|
510
|
+
onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody, precedingBreadcrumbs }) {
|
|
429
511
|
const urlPath = extractUrlPath(url);
|
|
430
512
|
const code = pgRestError.code;
|
|
431
513
|
const errorMessage = `${method} ${urlPath} -> ${statusCode} [PgRest:${code}]`;
|
|
@@ -448,7 +530,36 @@ var PostMessageSubscriber = class {
|
|
|
448
530
|
pgRestCode: code,
|
|
449
531
|
pgRestDetails: pgRestError.details ?? void 0,
|
|
450
532
|
pgRestHint: pgRestError.hint ?? void 0,
|
|
451
|
-
requestType: type
|
|
533
|
+
requestType: type,
|
|
534
|
+
precedingBreadcrumbs
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
this.send(msg, "*");
|
|
538
|
+
}
|
|
539
|
+
onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody, precedingBreadcrumbs }) {
|
|
540
|
+
const urlPath = extractUrlPath(url);
|
|
541
|
+
const code = authError.code;
|
|
542
|
+
const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
|
|
543
|
+
const msg = {
|
|
544
|
+
type: "GLOBAL_ERROR",
|
|
545
|
+
message: errorMessage,
|
|
546
|
+
error: null,
|
|
547
|
+
errorData: {
|
|
548
|
+
name: "AuthWhitelistError",
|
|
549
|
+
message: authError.message,
|
|
550
|
+
stack: null,
|
|
551
|
+
fileName: url,
|
|
552
|
+
lineNumber: 0,
|
|
553
|
+
columnNumber: 0,
|
|
554
|
+
httpPath: urlPath,
|
|
555
|
+
httpStatus: statusCode,
|
|
556
|
+
httpMethod: method,
|
|
557
|
+
requestBody: requestBody ?? "",
|
|
558
|
+
responseBody,
|
|
559
|
+
authCode: code,
|
|
560
|
+
authRequestId: authError.requestId ?? void 0,
|
|
561
|
+
requestType: type,
|
|
562
|
+
precedingBreadcrumbs
|
|
452
563
|
}
|
|
453
564
|
};
|
|
454
565
|
this.send(msg, "*");
|
|
@@ -468,17 +579,90 @@ function createNoopSubscriber() {
|
|
|
468
579
|
onNetworkRequest() {
|
|
469
580
|
},
|
|
470
581
|
onPgRestWhitelistError() {
|
|
582
|
+
},
|
|
583
|
+
onAuthWhitelistError() {
|
|
471
584
|
}
|
|
472
585
|
};
|
|
473
586
|
}
|
|
587
|
+
|
|
588
|
+
// src/subscribers/sentryEventSubscriber.ts
|
|
589
|
+
var import_core4 = require("@sentry/core");
|
|
590
|
+
function toSentryLevel(consoleLevel) {
|
|
591
|
+
switch (consoleLevel) {
|
|
592
|
+
case "error":
|
|
593
|
+
return "error";
|
|
594
|
+
case "warn":
|
|
595
|
+
return "warning";
|
|
596
|
+
case "debug":
|
|
597
|
+
return "debug";
|
|
598
|
+
case "info":
|
|
599
|
+
return "info";
|
|
600
|
+
default:
|
|
601
|
+
return "log";
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
var SentryEventSubscriber = class {
|
|
605
|
+
constructor(opts = {}) {
|
|
606
|
+
__publicField(this, "captureAllNetworkRequests");
|
|
607
|
+
this.captureAllNetworkRequests = opts.captureAllNetworkRequests ?? true;
|
|
608
|
+
}
|
|
609
|
+
onConsole(info) {
|
|
610
|
+
const { level, args } = info;
|
|
611
|
+
const formattedMessage = formatConsoleArgs(args);
|
|
612
|
+
const sentryLevel = toSentryLevel(level);
|
|
613
|
+
(0, import_core4.captureEvent)({
|
|
614
|
+
message: `[${level}]${formattedMessage}`,
|
|
615
|
+
level: sentryLevel,
|
|
616
|
+
// 按 level + 消息内容前 100 字符分组,同一类日志聚合为同一个 Issue
|
|
617
|
+
fingerprint: ["console", level, formattedMessage.slice(0, 100)],
|
|
618
|
+
tags: {
|
|
619
|
+
type: "console"
|
|
620
|
+
},
|
|
621
|
+
extra: { consoleType: level, args }
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
// Sentry 已自动捕获 JS 错误,不重复上报
|
|
625
|
+
onError(_info) {
|
|
626
|
+
}
|
|
627
|
+
onNetworkRequest(info) {
|
|
628
|
+
const { type, method, url, statusCode, requestBody, responseBody, isError, duration } = info;
|
|
629
|
+
const shouldCapture = this.captureAllNetworkRequests || isError || statusCode != null && statusCode >= 400;
|
|
630
|
+
if (!shouldCapture) return;
|
|
631
|
+
const urlPath = stripUrlOrigin(url);
|
|
632
|
+
const requestTypeLabel = type === "xhr" ? "XHR" : "Fetch";
|
|
633
|
+
const statusStr = statusCode != null ? String(statusCode) : "failed";
|
|
634
|
+
const durationStr = duration != null ? ` (${duration}ms)` : "";
|
|
635
|
+
const message = `${requestTypeLabel} ${method} ${urlPath} -> ${statusStr}${durationStr}`;
|
|
636
|
+
const level = isError ? "error" : statusCode != null && statusCode >= 400 ? "warning" : "info";
|
|
637
|
+
(0, import_core4.captureEvent)({
|
|
638
|
+
message,
|
|
639
|
+
level,
|
|
640
|
+
// 按 method + url 路径固定分组,同一接口的多次报错聚合为同一个 Issue
|
|
641
|
+
fingerprint: ["network", method, urlPath],
|
|
642
|
+
tags: {
|
|
643
|
+
type: "network"
|
|
644
|
+
},
|
|
645
|
+
extra: { url, method, statusCode, duration, requestBody, responseBody }
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
onPgRestWhitelistError(_info) {
|
|
649
|
+
}
|
|
650
|
+
onAuthWhitelistError(_info) {
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
function createSentryEventSubscriber(opts) {
|
|
654
|
+
return new SentryEventSubscriber(opts);
|
|
655
|
+
}
|
|
474
656
|
// Annotate the CommonJS export names for ESM import in node:
|
|
475
657
|
0 && (module.exports = {
|
|
658
|
+
DEFAULT_AUTH_URL_PATTERNS,
|
|
659
|
+
DEFAULT_AUTH_WHITELIST_CODES,
|
|
476
660
|
DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
477
661
|
DEFAULT_IGNORE_URLS,
|
|
478
662
|
DEFAULT_PGREST_URL_PATTERNS,
|
|
479
663
|
DEFAULT_PGREST_WHITELIST_CODES,
|
|
480
664
|
SentryCapture,
|
|
481
665
|
createNoopSubscriber,
|
|
482
|
-
createPostMessageSubscriber
|
|
666
|
+
createPostMessageSubscriber,
|
|
667
|
+
createSentryEventSubscriber
|
|
483
668
|
});
|
|
484
|
-
//# sourceMappingURL=index.cjs.map
|