miaoda-react-devkit 0.1.1-beta.21 → 0.1.1-beta.22

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/dist/index.js CHANGED
@@ -1,703 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
-
5
- // src/capture/console.ts
6
- import { addConsoleInstrumentationHandler } from "@sentry/core";
7
- function formatConsoleArg(arg) {
8
- if (typeof arg === "string") return arg;
9
- if (arg instanceof Error) return arg.message;
10
- try {
11
- return JSON.stringify(arg);
12
- } catch {
13
- return String(arg);
14
- }
15
- }
16
- function formatConsoleArgs(args) {
17
- return args.map(formatConsoleArg).join(" ");
18
- }
19
- var DEFAULT_CONSOLE_FILTER_KEYWORDS = [
20
- "Sentry initialized successfully",
21
- "[vite] ",
22
- "Download the React DevTools for a better development experience"
23
- ];
24
- function shouldFilterConsole(args, keywords) {
25
- if (keywords.length === 0) return false;
26
- const text = formatConsoleArgs(args);
27
- return keywords.some((kw) => text.includes(kw));
28
- }
29
- function shouldFilterConsoleMessage(message, keywords) {
30
- return keywords.some((kw) => message.includes(kw));
31
- }
32
- function createConsoleObserver(keywords, subscribers) {
33
- return {
34
- install() {
35
- if (subscribers.length === 0) return;
36
- addConsoleInstrumentationHandler(({ args, level }) => {
37
- if (shouldFilterConsole(args, keywords)) return;
38
- const info = { level, args };
39
- for (const sub of subscribers) sub.onConsole(info);
40
- });
41
- },
42
- beforeBreadcrumb(breadcrumb, hint) {
43
- if (breadcrumb.category === "console" && breadcrumb.message != null) {
44
- if (shouldFilterConsoleMessage(breadcrumb.message, keywords)) return null;
45
- const h = hint;
46
- const consoleType = h?.level ?? "log";
47
- const stacks = (h?.input ?? []).filter((arg) => arg instanceof Error && arg.stack != null).map((err) => err.stack);
48
- const originalMessage = breadcrumb.message;
49
- breadcrumb.message = `[${consoleType}]${originalMessage}`;
50
- breadcrumb.data = {
51
- ...breadcrumb.data ?? {},
52
- consoleType,
53
- message: originalMessage,
54
- ...stacks.length > 0 ? { stack: stacks.join("\n\n") } : {}
55
- };
56
- }
57
- return breadcrumb;
58
- }
59
- };
60
- }
61
-
62
- // src/capture/network.ts
63
- import { getClient, getIsolationScope } from "@sentry/core";
64
-
65
- // ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/debug-build.js
66
- var DEBUG_BUILD = typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__;
67
-
68
- // ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/networkUtils.js
69
- import { debug } from "@sentry/core";
70
- function serializeFormData(formData) {
71
- return new URLSearchParams(formData).toString();
72
- }
73
- function getBodyString(body, _debug = debug) {
74
- try {
75
- if (typeof body === "string") {
76
- return [body];
77
- }
78
- if (body instanceof URLSearchParams) {
79
- return [body.toString()];
80
- }
81
- if (body instanceof FormData) {
82
- return [serializeFormData(body)];
83
- }
84
- if (!body) {
85
- return [void 0];
86
- }
87
- } catch (error) {
88
- DEBUG_BUILD && _debug.error(error, "Failed to serialize body", body);
89
- return [void 0, "BODY_PARSE_ERROR"];
90
- }
91
- DEBUG_BUILD && _debug.log("Skipping network body because of body type", body);
92
- return [void 0, "UNPARSEABLE_BODY_TYPE"];
93
- }
94
- function getFetchRequestArgBody(fetchArgs = []) {
95
- if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== "object") {
96
- return void 0;
97
- }
98
- return fetchArgs[1].body;
99
- }
100
-
101
- // src/utils.ts
102
- function stripUrlOrigin(url) {
103
- try {
104
- const u = new URL(url);
105
- return u.pathname + u.search + u.hash;
106
- } catch {
107
- return url;
108
- }
109
- }
110
-
111
- // src/capture/pgrest.ts
112
- var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
113
- var DEFAULT_PGREST_WHITELIST_CODES = [
114
- "22P02",
115
- // 无效的文本表示(如类型转换失败)
116
- "42703",
117
- // 未定义的列
118
- "42P01",
119
- // 未定义的表
120
- "42501",
121
- // 权限不足
122
- "23503",
123
- // 外键约束失败
124
- "42P17",
125
- // 对象已存在
126
- "22008",
127
- // 日期时间字段值超出范围
128
- "23505",
129
- // 唯一键冲突
130
- "PGRST116",
131
- // 期望单行但返回多行
132
- "PGRST200",
133
- // 外键关系查找失败
134
- "PGRST204"
135
- // 响应中找不到列
136
- ];
137
- function isPgRestRequest(url, patterns) {
138
- return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
139
- }
140
- function parsePgRestErrorBody(body) {
141
- if (!body) return null;
142
- let parsed;
143
- try {
144
- parsed = JSON.parse(body);
145
- } catch {
146
- return null;
147
- }
148
- if (typeof parsed !== "object") return null;
149
- if (parsed === null) return null;
150
- const p = parsed;
151
- if (typeof p["code"] !== "string") return null;
152
- if (typeof p["message"] !== "string") return null;
153
- return {
154
- code: p["code"],
155
- message: p["message"],
156
- details: typeof p["details"] === "string" ? p["details"] : null,
157
- hint: typeof p["hint"] === "string" ? p["hint"] : null
158
- };
159
- }
160
-
161
- // src/capture/auth.ts
162
- var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
163
- var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
164
- function isAuthRequest(url, patterns) {
165
- return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
166
- }
167
- function parseAuthErrorBody(body) {
168
- if (!body) return null;
169
- let parsed;
170
- try {
171
- parsed = JSON.parse(body);
172
- } catch {
173
- return null;
174
- }
175
- if (typeof parsed !== "object") return null;
176
- if (parsed === null) return null;
177
- const p = parsed;
178
- if (typeof p["code"] !== "string") return null;
179
- if (typeof p["message"] !== "string") return null;
180
- return {
181
- code: p["code"],
182
- message: p["message"],
183
- requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
184
- };
185
- }
186
-
187
- // src/capture/storage.ts
188
- var DEFAULT_STORAGE_URL_PATTERNS = [/\/storage\/v1\//];
189
- var DEFAULT_STORAGE_WHITELIST_ERRORS = ["new row violates row-level security policy"];
190
- function isStorageRequest(url, patterns) {
191
- return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
192
- }
193
- function parseStorageErrorBody(body) {
194
- if (!body) return null;
195
- let parsed;
196
- try {
197
- parsed = JSON.parse(body);
198
- } catch {
199
- return null;
200
- }
201
- if (typeof parsed !== "object" || parsed === null) return null;
202
- const p = parsed;
203
- if (typeof p["statusCode"] !== "string") return null;
204
- if (typeof p["error"] !== "string") return null;
205
- if (typeof p["message"] !== "string") return null;
206
- return {
207
- statusCode: p["statusCode"],
208
- error: p["error"],
209
- message: p["message"]
210
- };
211
- }
212
-
213
- // src/capture/network.ts
214
- var BODY_TRUNCATE_LIMIT = 2e3;
215
- function calcDuration(hint) {
216
- if (hint?.startTimestamp == null || hint?.endTimestamp == null) return void 0;
217
- return Math.round(hint.endTimestamp - hint.startTimestamp);
218
- }
219
- var DEFAULT_IGNORE_URLS = [
220
- /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot|map)(\?.*)?$/,
221
- /\.chunk\./,
222
- /\/static\//,
223
- /\/assets\//
224
- ];
225
- function createUrlMatcher(patterns) {
226
- const regexes = patterns.map((p) => typeof p === "string" ? new RegExp(p) : p);
227
- return (url) => regexes.some((re) => re.test(url));
228
- }
229
- function truncateBody(body) {
230
- return body.length > BODY_TRUNCATE_LIMIT ? body.slice(0, BODY_TRUNCATE_LIMIT) + "... (truncated)" : body;
231
- }
232
- function isStreamByContentType(contentType) {
233
- return contentType.includes("text/event-stream") || contentType.includes("application/stream+json");
234
- }
235
- function handleXhrBreadcrumb(breadcrumb, hint, handler) {
236
- const data = breadcrumb.data;
237
- const method = data?.method ?? "GET";
238
- const url = data?.url ?? "";
239
- const statusCode = data?.status_code != null && data.status_code > 0 ? data.status_code : null;
240
- const isError = statusCode === null;
241
- const xhr = hint?.xhr;
242
- const [rawRequestBody] = getBodyString(hint?.input);
243
- const requestBody = rawRequestBody != null ? truncateBody(rawRequestBody) : void 0;
244
- let responseBody;
245
- if (xhr) {
246
- const contentType = xhr.getResponseHeader("content-type") ?? "";
247
- if (isStreamByContentType(contentType)) {
248
- responseBody = "[streaming response - not captured]";
249
- } else if (xhr.responseType === "" || xhr.responseType === "text") {
250
- try {
251
- responseBody = xhr.responseText != null ? truncateBody(xhr.responseText) : void 0;
252
- } catch {
253
- }
254
- } else if (xhr.responseType === "json") {
255
- try {
256
- responseBody = truncateBody(JSON.stringify(xhr.response));
257
- } catch {
258
- responseBody = "[json response parse failed]";
259
- }
260
- } else {
261
- responseBody = "[non-text response]";
262
- }
263
- }
264
- if (breadcrumb.data) {
265
- breadcrumb.data["requestBody"] = requestBody;
266
- breadcrumb.data["responseBody"] = responseBody;
267
- }
268
- const duration = calcDuration(hint);
269
- const relativeUrl = stripUrlOrigin(url);
270
- const statusStr = statusCode != null ? String(statusCode) : "failed";
271
- const durationStr = duration != null ? ` (${duration}ms)` : "";
272
- breadcrumb.message = `XHR ${method} ${relativeUrl} -> ${statusStr}${durationStr}`;
273
- if (breadcrumb.data) {
274
- breadcrumb.data["duration"] = duration;
275
- }
276
- void handler({ type: "xhr", method, url, statusCode, requestBody, responseBody, isError, duration });
277
- }
278
- async function handleFetchBreadcrumb(breadcrumb, hint, handler) {
279
- const data = breadcrumb.data;
280
- const method = data?.method ?? "GET";
281
- const url = data?.url ?? "";
282
- const statusCode = data?.status_code ?? null;
283
- const isError = hint?.data != null;
284
- const response = hint?.response;
285
- const cause = hint?.data instanceof Error ? hint.data : void 0;
286
- const reqBody = getFetchRequestArgBody(hint?.input ?? []);
287
- const [rawRequestBody] = getBodyString(reqBody);
288
- const requestBody = rawRequestBody != null ? truncateBody(rawRequestBody) : void 0;
289
- const duration = calcDuration(hint);
290
- const relativeUrl = stripUrlOrigin(url);
291
- const statusStr = statusCode != null ? String(statusCode) : "failed";
292
- const durationStr = duration != null ? ` (${duration}ms)` : "";
293
- breadcrumb.message = `Fetch ${method} ${relativeUrl} -> ${statusStr}${durationStr}`;
294
- if (breadcrumb.data) {
295
- breadcrumb.data["requestBody"] = requestBody;
296
- breadcrumb.data["duration"] = duration;
297
- }
298
- const contentType = response?.headers?.get("content-type") ?? "";
299
- if (isStreamByContentType(contentType)) {
300
- if (breadcrumb.data) {
301
- breadcrumb.data["responseBody"] = "[streaming response - not captured]";
302
- }
303
- await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody: "[streaming response - not captured]", isError, cause, duration });
304
- return;
305
- }
306
- let responseBody;
307
- if (response) {
308
- try {
309
- const text = await Promise.race([
310
- response.clone().text(),
311
- new Promise(
312
- (_, reject) => setTimeout(() => reject(new Error("timeout")), 500)
313
- )
314
- ]);
315
- responseBody = truncateBody(text);
316
- } catch {
317
- responseBody = void 0;
318
- }
319
- }
320
- if (breadcrumb.data) {
321
- breadcrumb.data["responseBody"] = responseBody;
322
- }
323
- await handler({ type: "fetch", method, url, statusCode, requestBody, responseBody, isError, cause, duration });
324
- }
325
- function createNetworkObserver(opts, subscribers) {
326
- const isIgnored = createUrlMatcher(opts.ignoreUrls);
327
- const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
328
- const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
329
- const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
330
- const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
331
- const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
332
- const storagePatterns = opts.storageUrlPatterns ?? DEFAULT_STORAGE_URL_PATTERNS;
333
- const storageWhitelistErrors = opts.storageWhitelistErrors ?? DEFAULT_STORAGE_WHITELIST_ERRORS;
334
- const shouldIgnore = (url) => {
335
- return isIgnored(url) && !isPgRestRequest(url, pgRestPatterns) && !isAuthRequest(url, authPatterns) && !isStorageRequest(url, storagePatterns);
336
- };
337
- function tryDispatchError(requestType, method, url, statusCode, requestBody, responseBody) {
338
- if (statusCode == null) return;
339
- const errorRequestType = isPgRestRequest(url, pgRestPatterns) ? "pgrest" : isAuthRequest(url, authPatterns) ? "auth" : isStorageRequest(url, storagePatterns) ? "storage" : null;
340
- if (!errorRequestType) return;
341
- const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
342
- const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
343
- if (errorRequestType === "pgrest") {
344
- const pgRestError = parsePgRestErrorBody(responseBody);
345
- if (!pgRestError) return;
346
- if (pgRestWhitelistCodes.length > 0 && !pgRestWhitelistCodes.includes(pgRestError.code)) return;
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
- } else if (errorRequestType === "auth") {
359
- const authError = parseAuthErrorBody(responseBody);
360
- if (!authError) return;
361
- if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
362
- const info = {
363
- type: requestType,
364
- method,
365
- url,
366
- statusCode,
367
- authError,
368
- requestBody,
369
- responseBody,
370
- precedingBreadcrumbs
371
- };
372
- for (const sub of subscribers) sub.onAuthWhitelistError(info);
373
- } else if (errorRequestType === "storage") {
374
- const storageError = parseStorageErrorBody(responseBody);
375
- if (!storageError) return;
376
- if (storageWhitelistErrors.length > 0 && !storageWhitelistErrors.includes(storageError.message)) return;
377
- const info = {
378
- type: requestType,
379
- method,
380
- url,
381
- statusCode,
382
- storageError,
383
- requestBody,
384
- responseBody,
385
- precedingBreadcrumbs
386
- };
387
- for (const sub of subscribers) sub.onStorageWhitelistError(info);
388
- }
389
- }
390
- function makeHandler(requestType) {
391
- return (info) => {
392
- for (const sub of subscribers) void sub.onNetworkRequest(info);
393
- tryDispatchError(
394
- requestType,
395
- info.method,
396
- info.url,
397
- info.statusCode,
398
- info.requestBody,
399
- info.responseBody
400
- );
401
- };
402
- }
403
- return {
404
- shouldIgnore,
405
- install() {
406
- if (subscribers.length === 0) return;
407
- const client = getClient();
408
- if (!client) return;
409
- const xhrHandler = makeHandler("xhr");
410
- const fetchHandler = makeHandler("fetch");
411
- client.on("beforeAddBreadcrumb", (breadcrumb, hint) => {
412
- const url = breadcrumb.data?.url ?? "";
413
- if (breadcrumb.category === "xhr") {
414
- if (shouldIgnore(url)) return;
415
- handleXhrBreadcrumb(breadcrumb, hint, xhrHandler);
416
- } else if (breadcrumb.category === "fetch") {
417
- if (shouldIgnore(url)) return;
418
- void handleFetchBreadcrumb(breadcrumb, hint, fetchHandler);
419
- }
420
- });
421
- }
422
- };
423
- }
424
-
425
- // src/capture/capture.ts
426
- var SentryCapture = class {
427
- constructor(opts, subscribers = []) {
428
- __publicField(this, "subscribers");
429
- __publicField(this, "console");
430
- __publicField(this, "network");
431
- this.subscribers = subscribers;
432
- this.console = createConsoleObserver(opts.consoleFilterKeywords, subscribers);
433
- this.network = createNetworkObserver(opts, subscribers);
434
- this.beforeSend = this.beforeSend.bind(this);
435
- this.beforeBreadcrumb = this.beforeBreadcrumb.bind(this);
436
- }
437
- /** 在 Sentry.init() 完成后调用一次,注册网络请求观测器。 */
438
- installNetworkObserver() {
439
- this.network.install();
440
- }
441
- /** 注册控制台日志观测器,可在 Sentry.init() 前后调用均可。 */
442
- installConsoleObserver() {
443
- this.console.install();
444
- }
445
- /** 传入 Sentry.init({ beforeSend }) —— 提取错误信息触发 onError 回调。 */
446
- beforeSend(event, hint) {
447
- const exceptionValue = event.exception?.values?.[0];
448
- const message = exceptionValue?.value ?? event.message ?? "(unknown)";
449
- const componentStack = event.contexts?.["react"]?.["componentStack"] ?? void 0;
450
- const mechanismType = exceptionValue?.mechanism?.type ?? null;
451
- const stack = hint.originalException instanceof Error ? hint.originalException.stack : void 0;
452
- const errorName = exceptionValue?.type ?? (hint.originalException instanceof Error ? hint.originalException.name : void 0);
453
- const frames = exceptionValue?.stacktrace?.frames;
454
- const innerFrame = frames?.length ? frames[frames.length - 1] : void 0;
455
- const fileName = innerFrame?.filename ?? innerFrame?.abs_path;
456
- const lineNumber = innerFrame?.lineno;
457
- const columnNumber = innerFrame?.colno;
458
- const info = { message, stack, componentStack, mechanismType, errorName, fileName, lineNumber, columnNumber };
459
- for (const sub of this.subscribers) sub.onError(info);
460
- return event;
461
- }
462
- /**
463
- * 传入 Sentry.init({ beforeBreadcrumb })。
464
- * - console:过滤噪音关键词
465
- * - fetch/xhr:过滤 ignoreUrls 匹配的静态资源请求
466
- */
467
- beforeBreadcrumb(breadcrumb, hint) {
468
- if (breadcrumb.category === "fetch" || breadcrumb.category === "xhr") {
469
- const url = breadcrumb.data?.url ?? "";
470
- if (this.network.shouldIgnore(url)) return null;
471
- }
472
- return this.console.beforeBreadcrumb(breadcrumb, hint);
473
- }
474
- };
475
-
476
- // src/subscribers/postMessageSubscriber.ts
477
- function extractUrlPath(url) {
478
- try {
479
- const u = new URL(url);
480
- return u.pathname + u.search + u.hash;
481
- } catch {
482
- return url;
483
- }
484
- }
485
- var PostMessageSubscriber = class {
486
- constructor(send = (msg, origin) => window.parent.postMessage(msg, origin)) {
487
- __publicField(this, "send");
488
- this.send = send;
489
- }
490
- // postMessage 场景不需要上报 console 日志
491
- onConsole(_info) {
492
- }
493
- onError({ message, stack, errorName, fileName, lineNumber, columnNumber }) {
494
- const name = errorName ?? "Error";
495
- const msg = {
496
- type: "GLOBAL_ERROR",
497
- message,
498
- error: stack != null ? { name, message, stack } : null,
499
- errorData: {
500
- name,
501
- message,
502
- stack,
503
- fileName: fileName ?? "unknown",
504
- lineNumber: lineNumber ?? 0,
505
- columnNumber: columnNumber ?? 0
506
- }
507
- };
508
- this.send(msg, "*");
509
- }
510
- // postMessage 场景不需要上报 network 日志
511
- onNetworkRequest(_info) {
512
- }
513
- onPgRestWhitelistError({ type, method, url, statusCode, pgRestError, requestBody, responseBody, precedingBreadcrumbs }) {
514
- const urlPath = extractUrlPath(url);
515
- const code = pgRestError.code;
516
- const errorMessage = `${method} ${urlPath} -> ${statusCode} [PgRest:${code}]`;
517
- const msg = {
518
- type: "GLOBAL_ERROR",
519
- message: errorMessage,
520
- error: null,
521
- errorData: {
522
- name: "PgRestWhitelistError",
523
- message: pgRestError.message,
524
- stack: null,
525
- fileName: url,
526
- lineNumber: 0,
527
- columnNumber: 0,
528
- httpPath: urlPath,
529
- httpStatus: statusCode,
530
- httpMethod: method,
531
- requestBody: requestBody ?? "",
532
- responseBody,
533
- pgRestCode: code,
534
- pgRestDetails: pgRestError.details ?? void 0,
535
- pgRestHint: pgRestError.hint ?? void 0,
536
- requestType: type,
537
- precedingBreadcrumbs
538
- }
539
- };
540
- this.send(msg, "*");
541
- }
542
- onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody, precedingBreadcrumbs }) {
543
- const urlPath = extractUrlPath(url);
544
- const code = authError.code;
545
- const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
546
- const msg = {
547
- type: "GLOBAL_ERROR",
548
- message: errorMessage,
549
- error: null,
550
- errorData: {
551
- name: "AuthWhitelistError",
552
- message: authError.message,
553
- stack: null,
554
- fileName: url,
555
- lineNumber: 0,
556
- columnNumber: 0,
557
- httpPath: urlPath,
558
- httpStatus: statusCode,
559
- httpMethod: method,
560
- requestBody: requestBody ?? "",
561
- responseBody,
562
- authCode: code,
563
- authRequestId: authError.requestId ?? void 0,
564
- requestType: type,
565
- precedingBreadcrumbs
566
- }
567
- };
568
- this.send(msg, "*");
569
- }
570
- onStorageWhitelistError({ type, method, url, statusCode, storageError, requestBody, responseBody, precedingBreadcrumbs }) {
571
- const urlPath = extractUrlPath(url);
572
- const errorMessage = `${method} ${urlPath} -> ${statusCode} [Storage:${storageError.error}]`;
573
- const msg = {
574
- type: "GLOBAL_ERROR",
575
- message: errorMessage,
576
- error: null,
577
- errorData: {
578
- name: "StorageWhitelistError",
579
- message: storageError.message,
580
- stack: null,
581
- fileName: url,
582
- lineNumber: 0,
583
- columnNumber: 0,
584
- httpPath: urlPath,
585
- httpStatus: statusCode,
586
- httpMethod: method,
587
- requestBody: requestBody ?? "",
588
- responseBody,
589
- storageError: storageError.error,
590
- storageStatusCode: storageError.statusCode,
591
- requestType: type,
592
- precedingBreadcrumbs
593
- }
594
- };
595
- this.send(msg, "*");
596
- }
597
- };
598
- function createPostMessageSubscriber(send) {
599
- return new PostMessageSubscriber(send);
600
- }
601
-
602
- // src/subscribers/noopSubscriber.ts
603
- function createNoopSubscriber() {
604
- return {
605
- onConsole() {
606
- },
607
- onError() {
608
- },
609
- onNetworkRequest() {
610
- },
611
- onPgRestWhitelistError() {
612
- },
613
- onAuthWhitelistError() {
614
- },
615
- onStorageWhitelistError() {
616
- }
617
- };
618
- }
619
-
620
- // src/subscribers/sentryEventSubscriber.ts
621
- import { captureEvent } from "@sentry/core";
622
- function toSentryLevel(consoleLevel) {
623
- switch (consoleLevel) {
624
- case "error":
625
- return "error";
626
- case "warn":
627
- return "warning";
628
- case "debug":
629
- return "debug";
630
- case "info":
631
- return "info";
632
- default:
633
- return "log";
634
- }
635
- }
636
- var SentryEventSubscriber = class {
637
- constructor(opts = {}) {
638
- __publicField(this, "captureAllNetworkRequests");
639
- this.captureAllNetworkRequests = opts.captureAllNetworkRequests ?? true;
640
- }
641
- onConsole(info) {
642
- const { level, args } = info;
643
- const formattedMessage = formatConsoleArgs(args);
644
- const sentryLevel = toSentryLevel(level);
645
- captureEvent({
646
- message: `[${level}]${formattedMessage}`,
647
- level: sentryLevel,
648
- // 按 level + 消息内容前 100 字符分组,同一类日志聚合为同一个 Issue
649
- fingerprint: ["console", level, formattedMessage.slice(0, 100)],
650
- tags: {
651
- type: "console"
652
- },
653
- extra: { consoleType: level, args }
654
- });
655
- }
656
- // Sentry 已自动捕获 JS 错误,不重复上报
657
- onError(_info) {
658
- }
659
- onNetworkRequest(info) {
660
- const { type, method, url, statusCode, requestBody, responseBody, isError, duration } = info;
661
- const shouldCapture = this.captureAllNetworkRequests || isError || statusCode != null && statusCode >= 400;
662
- if (!shouldCapture) return;
663
- const urlPath = stripUrlOrigin(url);
664
- const requestTypeLabel = type === "xhr" ? "XHR" : "Fetch";
665
- const statusStr = statusCode != null ? String(statusCode) : "failed";
666
- const durationStr = duration != null ? ` (${duration}ms)` : "";
667
- const message = `${requestTypeLabel} ${method} ${urlPath} -> ${statusStr}${durationStr}`;
668
- const level = isError ? "error" : statusCode != null && statusCode >= 400 ? "warning" : "info";
669
- captureEvent({
670
- message,
671
- level,
672
- // 按 method + url 路径固定分组,同一接口的多次报错聚合为同一个 Issue
673
- fingerprint: ["network", method, urlPath],
674
- tags: {
675
- type: "network"
676
- },
677
- extra: { url, method, statusCode, duration, requestBody, responseBody }
678
- });
679
- }
680
- onPgRestWhitelistError(_info) {
681
- }
682
- onAuthWhitelistError(_info) {
683
- }
684
- onStorageWhitelistError(_info) {
685
- }
686
- };
687
- function createSentryEventSubscriber(opts) {
688
- return new SentryEventSubscriber(opts);
689
- }
690
- export {
691
- DEFAULT_AUTH_URL_PATTERNS,
692
- DEFAULT_AUTH_WHITELIST_CODES,
693
- DEFAULT_CONSOLE_FILTER_KEYWORDS,
694
- DEFAULT_IGNORE_URLS,
695
- DEFAULT_PGREST_URL_PATTERNS,
696
- DEFAULT_PGREST_WHITELIST_CODES,
697
- DEFAULT_STORAGE_URL_PATTERNS,
698
- DEFAULT_STORAGE_WHITELIST_ERRORS,
699
- SentryCapture,
700
- createNoopSubscriber,
701
- createPostMessageSubscriber,
702
- createSentryEventSubscriber
703
- };