miaoda-react-devkit 0.1.1-beta.0 → 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 +622 -47
- package/dist/index.d.cts +305 -41
- package/dist/index.d.ts +305 -41
- package/dist/index.js +613 -49
- package/dist/stubs/sentry-react-noop-stub.js +78 -0
- package/dist/stubs/sentry-react-stub.js +547 -86
- package/dist/vite/index.cjs +80 -1
- package/dist/vite/index.d.cts +15 -1
- package/dist/vite/index.d.ts +15 -1
- package/dist/vite/index.js +78 -1
- package/package.json +8 -1
- package/README.md +0 -41
package/dist/index.cjs
CHANGED
|
@@ -22,72 +22,647 @@ 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
|
-
|
|
25
|
+
DEFAULT_AUTH_URL_PATTERNS: () => DEFAULT_AUTH_URL_PATTERNS,
|
|
26
|
+
DEFAULT_AUTH_WHITELIST_CODES: () => DEFAULT_AUTH_WHITELIST_CODES,
|
|
27
|
+
DEFAULT_CONSOLE_FILTER_KEYWORDS: () => DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
28
|
+
DEFAULT_IGNORE_URLS: () => DEFAULT_IGNORE_URLS,
|
|
29
|
+
DEFAULT_PGREST_URL_PATTERNS: () => DEFAULT_PGREST_URL_PATTERNS,
|
|
30
|
+
DEFAULT_PGREST_WHITELIST_CODES: () => DEFAULT_PGREST_WHITELIST_CODES,
|
|
31
|
+
SentryCapture: () => SentryCapture,
|
|
32
|
+
createNoopSubscriber: () => createNoopSubscriber,
|
|
33
|
+
createPostMessageSubscriber: () => createPostMessageSubscriber,
|
|
34
|
+
createSentryEventSubscriber: () => createSentryEventSubscriber
|
|
26
35
|
});
|
|
27
36
|
module.exports = __toCommonJS(src_exports);
|
|
28
37
|
|
|
29
|
-
// src/capture.ts
|
|
38
|
+
// src/capture/console.ts
|
|
30
39
|
var import_core = require("@sentry/core");
|
|
40
|
+
function formatConsoleArg(arg) {
|
|
41
|
+
if (typeof arg === "string") return arg;
|
|
42
|
+
if (arg instanceof Error) return arg.message;
|
|
43
|
+
try {
|
|
44
|
+
return JSON.stringify(arg);
|
|
45
|
+
} catch {
|
|
46
|
+
return String(arg);
|
|
47
|
+
}
|
|
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
|
+
];
|
|
57
|
+
function shouldFilterConsole(args, keywords) {
|
|
58
|
+
if (keywords.length === 0) return false;
|
|
59
|
+
const text = formatConsoleArgs(args);
|
|
60
|
+
return keywords.some((kw) => text.includes(kw));
|
|
61
|
+
}
|
|
62
|
+
function shouldFilterConsoleMessage(message, keywords) {
|
|
63
|
+
return keywords.some((kw) => message.includes(kw));
|
|
64
|
+
}
|
|
65
|
+
function createConsoleObserver(keywords, subscribers) {
|
|
66
|
+
return {
|
|
67
|
+
install() {
|
|
68
|
+
if (subscribers.length === 0) return;
|
|
69
|
+
(0, import_core.addConsoleInstrumentationHandler)(({ args, level }) => {
|
|
70
|
+
if (shouldFilterConsole(args, keywords)) return;
|
|
71
|
+
const info = { level, args };
|
|
72
|
+
for (const sub of subscribers) sub.onConsole(info);
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
beforeBreadcrumb(breadcrumb, hint) {
|
|
76
|
+
if (breadcrumb.category === "console" && breadcrumb.message != null) {
|
|
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
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return breadcrumb;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/capture/network.ts
|
|
96
|
+
var import_core3 = require("@sentry/core");
|
|
97
|
+
|
|
98
|
+
// ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/debug-build.js
|
|
99
|
+
var DEBUG_BUILD = typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__;
|
|
100
|
+
|
|
101
|
+
// ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/networkUtils.js
|
|
102
|
+
var import_core2 = require("@sentry/core");
|
|
103
|
+
function serializeFormData(formData) {
|
|
104
|
+
return new URLSearchParams(formData).toString();
|
|
105
|
+
}
|
|
106
|
+
function getBodyString(body, _debug = import_core2.debug) {
|
|
107
|
+
try {
|
|
108
|
+
if (typeof body === "string") {
|
|
109
|
+
return [body];
|
|
110
|
+
}
|
|
111
|
+
if (body instanceof URLSearchParams) {
|
|
112
|
+
return [body.toString()];
|
|
113
|
+
}
|
|
114
|
+
if (body instanceof FormData) {
|
|
115
|
+
return [serializeFormData(body)];
|
|
116
|
+
}
|
|
117
|
+
if (!body) {
|
|
118
|
+
return [void 0];
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
DEBUG_BUILD && _debug.error(error, "Failed to serialize body", body);
|
|
122
|
+
return [void 0, "BODY_PARSE_ERROR"];
|
|
123
|
+
}
|
|
124
|
+
DEBUG_BUILD && _debug.log("Skipping network body because of body type", body);
|
|
125
|
+
return [void 0, "UNPARSEABLE_BODY_TYPE"];
|
|
126
|
+
}
|
|
127
|
+
function getFetchRequestArgBody(fetchArgs = []) {
|
|
128
|
+
if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== "object") {
|
|
129
|
+
return void 0;
|
|
130
|
+
}
|
|
131
|
+
return fetchArgs[1].body;
|
|
132
|
+
}
|
|
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
|
+
|
|
144
|
+
// src/capture/pgrest.ts
|
|
145
|
+
var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
|
|
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
|
+
];
|
|
170
|
+
function isPgRestRequest(url, patterns) {
|
|
171
|
+
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
172
|
+
}
|
|
173
|
+
function parsePgRestErrorBody(body) {
|
|
174
|
+
if (!body) return null;
|
|
175
|
+
let parsed;
|
|
176
|
+
try {
|
|
177
|
+
parsed = JSON.parse(body);
|
|
178
|
+
} catch {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (typeof parsed !== "object") return null;
|
|
182
|
+
if (parsed === null) return null;
|
|
183
|
+
const p = parsed;
|
|
184
|
+
if (typeof p["code"] !== "string") return null;
|
|
185
|
+
if (typeof p["message"] !== "string") return null;
|
|
186
|
+
return {
|
|
187
|
+
code: p["code"],
|
|
188
|
+
message: p["message"],
|
|
189
|
+
details: typeof p["details"] === "string" ? p["details"] : null,
|
|
190
|
+
hint: typeof p["hint"] === "string" ? p["hint"] : null
|
|
191
|
+
};
|
|
192
|
+
}
|
|
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
|
+
|
|
220
|
+
// src/capture/network.ts
|
|
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
|
+
}
|
|
226
|
+
var DEFAULT_IGNORE_URLS = [
|
|
227
|
+
/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot|map)(\?.*)?$/,
|
|
228
|
+
/\.chunk\./,
|
|
229
|
+
/\/static\//,
|
|
230
|
+
/\/assets\//
|
|
231
|
+
];
|
|
232
|
+
function createUrlMatcher(patterns) {
|
|
233
|
+
const regexes = patterns.map((p) => typeof p === "string" ? new RegExp(p) : p);
|
|
234
|
+
return (url) => regexes.some((re) => re.test(url));
|
|
235
|
+
}
|
|
236
|
+
function truncateBody(body) {
|
|
237
|
+
return body.length > BODY_TRUNCATE_LIMIT ? body.slice(0, BODY_TRUNCATE_LIMIT) + "... (truncated)" : body;
|
|
238
|
+
}
|
|
239
|
+
function isStreamByContentType(contentType) {
|
|
240
|
+
return contentType.includes("text/event-stream") || contentType.includes("application/stream+json");
|
|
241
|
+
}
|
|
242
|
+
function handleXhrBreadcrumb(breadcrumb, hint, handler) {
|
|
243
|
+
const data = breadcrumb.data;
|
|
244
|
+
const method = data?.method ?? "GET";
|
|
245
|
+
const url = data?.url ?? "";
|
|
246
|
+
const statusCode = data?.status_code != null && data.status_code > 0 ? data.status_code : null;
|
|
247
|
+
const isError = statusCode === null;
|
|
248
|
+
const xhr = hint?.xhr;
|
|
249
|
+
const [rawRequestBody] = getBodyString(hint?.input);
|
|
250
|
+
const requestBody = rawRequestBody != null ? truncateBody(rawRequestBody) : void 0;
|
|
251
|
+
let responseBody;
|
|
252
|
+
if (xhr) {
|
|
253
|
+
const contentType = xhr.getResponseHeader("content-type") ?? "";
|
|
254
|
+
if (isStreamByContentType(contentType)) {
|
|
255
|
+
responseBody = "[streaming response - not captured]";
|
|
256
|
+
} else if (xhr.responseType === "" || xhr.responseType === "text") {
|
|
257
|
+
try {
|
|
258
|
+
responseBody = xhr.responseText != null ? truncateBody(xhr.responseText) : void 0;
|
|
259
|
+
} catch {
|
|
260
|
+
}
|
|
261
|
+
} else if (xhr.responseType === "json") {
|
|
262
|
+
try {
|
|
263
|
+
responseBody = truncateBody(JSON.stringify(xhr.response));
|
|
264
|
+
} catch {
|
|
265
|
+
responseBody = "[json response parse failed]";
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
responseBody = "[non-text response]";
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (breadcrumb.data) {
|
|
272
|
+
breadcrumb.data["requestBody"] = requestBody;
|
|
273
|
+
breadcrumb.data["responseBody"] = responseBody;
|
|
274
|
+
}
|
|
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 });
|
|
284
|
+
}
|
|
285
|
+
async function handleFetchBreadcrumb(breadcrumb, hint, handler) {
|
|
286
|
+
const data = breadcrumb.data;
|
|
287
|
+
const method = data?.method ?? "GET";
|
|
288
|
+
const url = data?.url ?? "";
|
|
289
|
+
const statusCode = data?.status_code ?? null;
|
|
290
|
+
const isError = hint?.data != null;
|
|
291
|
+
const response = hint?.response;
|
|
292
|
+
const cause = hint?.data instanceof Error ? hint.data : void 0;
|
|
293
|
+
const reqBody = getFetchRequestArgBody(hint?.input ?? []);
|
|
294
|
+
const [rawRequestBody] = getBodyString(reqBody);
|
|
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}`;
|
|
301
|
+
if (breadcrumb.data) {
|
|
302
|
+
breadcrumb.data["requestBody"] = requestBody;
|
|
303
|
+
breadcrumb.data["duration"] = duration;
|
|
304
|
+
}
|
|
305
|
+
const contentType = response?.headers?.get("content-type") ?? "";
|
|
306
|
+
if (isStreamByContentType(contentType)) {
|
|
307
|
+
if (breadcrumb.data) {
|
|
308
|
+
breadcrumb.data["responseBody"] = "[streaming response - not captured]";
|
|
309
|
+
}
|
|
310
|
+
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody: "[streaming response - not captured]", isError, cause, duration });
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
let responseBody;
|
|
314
|
+
if (response) {
|
|
315
|
+
try {
|
|
316
|
+
const text = await Promise.race([
|
|
317
|
+
response.clone().text(),
|
|
318
|
+
new Promise(
|
|
319
|
+
(_, reject) => setTimeout(() => reject(new Error("timeout")), 500)
|
|
320
|
+
)
|
|
321
|
+
]);
|
|
322
|
+
responseBody = truncateBody(text);
|
|
323
|
+
} catch {
|
|
324
|
+
responseBody = void 0;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (breadcrumb.data) {
|
|
328
|
+
breadcrumb.data["responseBody"] = responseBody;
|
|
329
|
+
}
|
|
330
|
+
await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody, isError, cause, duration });
|
|
331
|
+
}
|
|
332
|
+
function createNetworkObserver(opts, subscribers) {
|
|
333
|
+
const shouldIgnore = createUrlMatcher(opts.ignoreUrls);
|
|
334
|
+
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
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;
|
|
339
|
+
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
340
|
+
if (statusCode == null) return;
|
|
341
|
+
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
342
|
+
const pgRestError = parsePgRestErrorBody(responseBody);
|
|
343
|
+
if (!pgRestError) return;
|
|
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();
|
|
347
|
+
const info = {
|
|
348
|
+
type: requestType,
|
|
349
|
+
method,
|
|
350
|
+
url,
|
|
351
|
+
statusCode,
|
|
352
|
+
pgRestError,
|
|
353
|
+
requestBody,
|
|
354
|
+
responseBody,
|
|
355
|
+
precedingBreadcrumbs
|
|
356
|
+
};
|
|
357
|
+
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
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
|
+
}
|
|
379
|
+
function makeHandler(requestType) {
|
|
380
|
+
return (info) => {
|
|
381
|
+
for (const sub of subscribers) void sub.onNetworkRequest(info);
|
|
382
|
+
tryDispatchPgRestWhitelistError(
|
|
383
|
+
requestType,
|
|
384
|
+
info.method,
|
|
385
|
+
info.url,
|
|
386
|
+
info.statusCode,
|
|
387
|
+
info.requestBody,
|
|
388
|
+
info.responseBody
|
|
389
|
+
);
|
|
390
|
+
tryDispatchAuthWhitelistError(
|
|
391
|
+
requestType,
|
|
392
|
+
info.method,
|
|
393
|
+
info.url,
|
|
394
|
+
info.statusCode,
|
|
395
|
+
info.requestBody,
|
|
396
|
+
info.responseBody
|
|
397
|
+
);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
shouldIgnore,
|
|
402
|
+
install() {
|
|
403
|
+
if (subscribers.length === 0) return;
|
|
404
|
+
const client = (0, import_core3.getClient)();
|
|
405
|
+
if (!client) return;
|
|
406
|
+
const xhrHandler = makeHandler("xhr");
|
|
407
|
+
const fetchHandler = makeHandler("fetch");
|
|
408
|
+
client.on("beforeAddBreadcrumb", (breadcrumb, hint) => {
|
|
409
|
+
const url = breadcrumb.data?.url ?? "";
|
|
410
|
+
if (breadcrumb.category === "xhr") {
|
|
411
|
+
if (shouldIgnore(url)) return;
|
|
412
|
+
handleXhrBreadcrumb(breadcrumb, hint, xhrHandler);
|
|
413
|
+
} else if (breadcrumb.category === "fetch") {
|
|
414
|
+
if (shouldIgnore(url)) return;
|
|
415
|
+
void handleFetchBreadcrumb(breadcrumb, hint, fetchHandler);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/capture/capture.ts
|
|
31
423
|
var SentryCapture = class {
|
|
32
|
-
constructor(
|
|
33
|
-
__publicField(this, "
|
|
34
|
-
__publicField(this, "
|
|
35
|
-
__publicField(this, "
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
424
|
+
constructor(opts, subscribers = []) {
|
|
425
|
+
__publicField(this, "subscribers");
|
|
426
|
+
__publicField(this, "console");
|
|
427
|
+
__publicField(this, "network");
|
|
428
|
+
this.subscribers = subscribers;
|
|
429
|
+
this.console = createConsoleObserver(opts.consoleFilterKeywords, subscribers);
|
|
430
|
+
this.network = createNetworkObserver(opts, subscribers);
|
|
39
431
|
this.beforeSend = this.beforeSend.bind(this);
|
|
40
432
|
this.beforeBreadcrumb = this.beforeBreadcrumb.bind(this);
|
|
41
433
|
}
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
installFetchObserver() {
|
|
47
|
-
if (!this.onRequest) return;
|
|
48
|
-
const handler = this.onRequest;
|
|
49
|
-
(0, import_core.addFetchInstrumentationHandler)((data) => {
|
|
50
|
-
const { method, url } = data.fetchData;
|
|
51
|
-
const responseContentType = data.response?.headers?.get?.("content-type") ?? "";
|
|
52
|
-
if (responseContentType.includes("text/event-stream")) return;
|
|
53
|
-
if (!data.endTimestamp) {
|
|
54
|
-
handler({ method, url, statusCode: null, isEnd: false, isError: false });
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const statusCode = data.response?.status ?? null;
|
|
58
|
-
const isError = data.error != null;
|
|
59
|
-
handler({ method, url, statusCode, isEnd: true, isError });
|
|
60
|
-
});
|
|
434
|
+
/** 在 Sentry.init() 完成后调用一次,注册网络请求观测器。 */
|
|
435
|
+
installNetworkObserver() {
|
|
436
|
+
this.network.install();
|
|
61
437
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
438
|
+
/** 注册控制台日志观测器,可在 Sentry.init() 前后调用均可。 */
|
|
439
|
+
installConsoleObserver() {
|
|
440
|
+
this.console.install();
|
|
441
|
+
}
|
|
442
|
+
/** 传入 Sentry.init({ beforeSend }) —— 提取错误信息触发 onError 回调。 */
|
|
66
443
|
beforeSend(event, hint) {
|
|
67
|
-
const
|
|
444
|
+
const exceptionValue = event.exception?.values?.[0];
|
|
445
|
+
const message = exceptionValue?.value ?? event.message ?? "(unknown)";
|
|
68
446
|
const componentStack = event.contexts?.["react"]?.["componentStack"] ?? void 0;
|
|
69
|
-
const mechanismType =
|
|
447
|
+
const mechanismType = exceptionValue?.mechanism?.type ?? null;
|
|
70
448
|
const stack = hint.originalException instanceof Error ? hint.originalException.stack : void 0;
|
|
71
|
-
|
|
449
|
+
const errorName = exceptionValue?.type ?? (hint.originalException instanceof Error ? hint.originalException.name : void 0);
|
|
450
|
+
const frames = exceptionValue?.stacktrace?.frames;
|
|
451
|
+
const innerFrame = frames?.length ? frames[frames.length - 1] : void 0;
|
|
452
|
+
const fileName = innerFrame?.filename ?? innerFrame?.abs_path;
|
|
453
|
+
const lineNumber = innerFrame?.lineno;
|
|
454
|
+
const columnNumber = innerFrame?.colno;
|
|
455
|
+
const info = { message, stack, componentStack, mechanismType, errorName, fileName, lineNumber, columnNumber };
|
|
456
|
+
for (const sub of this.subscribers) sub.onError(info);
|
|
72
457
|
return event;
|
|
73
458
|
}
|
|
74
459
|
/**
|
|
75
|
-
*
|
|
460
|
+
* 传入 Sentry.init({ beforeBreadcrumb })。
|
|
461
|
+
* - console:过滤噪音关键词
|
|
462
|
+
* - fetch/xhr:过滤 ignoreUrls 匹配的静态资源请求
|
|
76
463
|
*/
|
|
77
|
-
beforeBreadcrumb(breadcrumb,
|
|
78
|
-
if (breadcrumb.
|
|
79
|
-
const
|
|
80
|
-
this.
|
|
81
|
-
method: data?.method ?? null,
|
|
82
|
-
url: data?.url ?? null,
|
|
83
|
-
statusCode: data?.status_code ?? null
|
|
84
|
-
});
|
|
464
|
+
beforeBreadcrumb(breadcrumb, hint) {
|
|
465
|
+
if (breadcrumb.category === "fetch" || breadcrumb.category === "xhr") {
|
|
466
|
+
const url = breadcrumb.data?.url ?? "";
|
|
467
|
+
if (this.network.shouldIgnore(url)) return null;
|
|
85
468
|
}
|
|
86
|
-
return breadcrumb;
|
|
469
|
+
return this.console.beforeBreadcrumb(breadcrumb, hint);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
// src/subscribers/postMessageSubscriber.ts
|
|
474
|
+
function extractUrlPath(url) {
|
|
475
|
+
try {
|
|
476
|
+
const u = new URL(url);
|
|
477
|
+
return u.pathname + u.search + u.hash;
|
|
478
|
+
} catch {
|
|
479
|
+
return url;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
var PostMessageSubscriber = class {
|
|
483
|
+
constructor(send = (msg, origin) => window.parent.postMessage(msg, origin)) {
|
|
484
|
+
__publicField(this, "send");
|
|
485
|
+
this.send = send;
|
|
486
|
+
}
|
|
487
|
+
// postMessage 场景不需要上报 console 日志
|
|
488
|
+
onConsole(_info) {
|
|
489
|
+
}
|
|
490
|
+
onError({ message, stack, errorName, fileName, lineNumber, columnNumber }) {
|
|
491
|
+
const name = errorName ?? "Error";
|
|
492
|
+
const msg = {
|
|
493
|
+
type: "GLOBAL_ERROR",
|
|
494
|
+
message,
|
|
495
|
+
error: stack != null ? { name, message, stack } : null,
|
|
496
|
+
errorData: {
|
|
497
|
+
name,
|
|
498
|
+
message,
|
|
499
|
+
stack,
|
|
500
|
+
fileName: fileName ?? "unknown",
|
|
501
|
+
lineNumber: lineNumber ?? 0,
|
|
502
|
+
columnNumber: columnNumber ?? 0
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
this.send(msg, "*");
|
|
506
|
+
}
|
|
507
|
+
// postMessage 场景不需要上报 network 日志
|
|
508
|
+
onNetworkRequest(_info) {
|
|
509
|
+
}
|
|
510
|
+
onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody, precedingBreadcrumbs }) {
|
|
511
|
+
const urlPath = extractUrlPath(url);
|
|
512
|
+
const code = pgRestError.code;
|
|
513
|
+
const errorMessage = `${method} ${urlPath} -> ${statusCode} [PgRest:${code}]`;
|
|
514
|
+
const msg = {
|
|
515
|
+
type: "GLOBAL_ERROR",
|
|
516
|
+
message: errorMessage,
|
|
517
|
+
error: null,
|
|
518
|
+
errorData: {
|
|
519
|
+
name: "PgRestWhitelistError",
|
|
520
|
+
message: pgRestError.message,
|
|
521
|
+
stack: null,
|
|
522
|
+
fileName: url,
|
|
523
|
+
lineNumber: 0,
|
|
524
|
+
columnNumber: 0,
|
|
525
|
+
httpPath: urlPath,
|
|
526
|
+
httpStatus: statusCode,
|
|
527
|
+
httpMethod: method,
|
|
528
|
+
requestBody: requestBody ?? "",
|
|
529
|
+
responseBody,
|
|
530
|
+
pgRestCode: code,
|
|
531
|
+
pgRestDetails: pgRestError.details ?? void 0,
|
|
532
|
+
pgRestHint: pgRestError.hint ?? void 0,
|
|
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
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
this.send(msg, "*");
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
function createPostMessageSubscriber(send) {
|
|
569
|
+
return new PostMessageSubscriber(send);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// src/subscribers/noopSubscriber.ts
|
|
573
|
+
function createNoopSubscriber() {
|
|
574
|
+
return {
|
|
575
|
+
onConsole() {
|
|
576
|
+
},
|
|
577
|
+
onError() {
|
|
578
|
+
},
|
|
579
|
+
onNetworkRequest() {
|
|
580
|
+
},
|
|
581
|
+
onPgRestWhitelistError() {
|
|
582
|
+
},
|
|
583
|
+
onAuthWhitelistError() {
|
|
584
|
+
}
|
|
585
|
+
};
|
|
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) {
|
|
87
651
|
}
|
|
88
652
|
};
|
|
653
|
+
function createSentryEventSubscriber(opts) {
|
|
654
|
+
return new SentryEventSubscriber(opts);
|
|
655
|
+
}
|
|
89
656
|
// Annotate the CommonJS export names for ESM import in node:
|
|
90
657
|
0 && (module.exports = {
|
|
91
|
-
|
|
658
|
+
DEFAULT_AUTH_URL_PATTERNS,
|
|
659
|
+
DEFAULT_AUTH_WHITELIST_CODES,
|
|
660
|
+
DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
661
|
+
DEFAULT_IGNORE_URLS,
|
|
662
|
+
DEFAULT_PGREST_URL_PATTERNS,
|
|
663
|
+
DEFAULT_PGREST_WHITELIST_CODES,
|
|
664
|
+
SentryCapture,
|
|
665
|
+
createNoopSubscriber,
|
|
666
|
+
createPostMessageSubscriber,
|
|
667
|
+
createSentryEventSubscriber
|
|
92
668
|
});
|
|
93
|
-
//# sourceMappingURL=index.cjs.map
|