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