@qiu_jun/monitors 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +90 -0
  2. package/dist/exception/index.d.ts +3 -0
  3. package/dist/exception/index.js +9 -0
  4. package/dist/exception/reporter.d.ts +18 -0
  5. package/dist/exception/reporter.js +106 -0
  6. package/dist/exception/transport.d.ts +3 -0
  7. package/dist/exception/transport.js +16 -0
  8. package/dist/exception/types.d.ts +33 -0
  9. package/dist/exception/types.js +6 -0
  10. package/dist/h5.d.ts +4 -0
  11. package/dist/h5.js +11 -0
  12. package/dist/index.d.ts +4 -0
  13. package/dist/index.js +13 -0
  14. package/dist/interface/index.d.ts +83 -0
  15. package/dist/interface/index.js +2 -0
  16. package/dist/modules/err.d.ts +1 -0
  17. package/dist/modules/err.js +91 -0
  18. package/dist/modules/index.d.ts +5 -0
  19. package/dist/modules/index.js +16 -0
  20. package/dist/modules/longTask.d.ts +1 -0
  21. package/dist/modules/longTask.js +27 -0
  22. package/dist/modules/paint.d.ts +1 -0
  23. package/dist/modules/paint.js +61 -0
  24. package/dist/modules/timing.d.ts +1 -0
  25. package/dist/modules/timing.js +42 -0
  26. package/dist/modules/xhr.d.ts +1 -0
  27. package/dist/modules/xhr.js +81 -0
  28. package/dist/monitor.d.ts +43 -0
  29. package/dist/monitor.js +176 -0
  30. package/dist/monitor.umd.js +1 -0
  31. package/dist/reporter.d.ts +3 -0
  32. package/dist/reporter.js +9 -0
  33. package/dist/utils/checkWhiteScreen.d.ts +1 -0
  34. package/dist/utils/checkWhiteScreen.js +42 -0
  35. package/dist/utils/formatStack.d.ts +1 -0
  36. package/dist/utils/formatStack.js +17 -0
  37. package/dist/utils/getLastEvent.d.ts +2 -0
  38. package/dist/utils/getLastEvent.js +29 -0
  39. package/dist/utils/getLogBaseData.d.ts +5 -0
  40. package/dist/utils/getLogBaseData.js +19 -0
  41. package/dist/utils/getSelector.d.ts +1 -0
  42. package/dist/utils/getSelector.js +45 -0
  43. package/dist/utils/index.d.ts +7 -0
  44. package/dist/utils/index.js +22 -0
  45. package/dist/utils/parseQueryString.d.ts +1 -0
  46. package/dist/utils/parseQueryString.js +12 -0
  47. package/dist/utils/toExcEvent.d.ts +8 -0
  48. package/dist/utils/toExcEvent.js +149 -0
  49. package/package.json +57 -0
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mapRequestLog = exports.isExceptionReportUrl = exports.toExcEvents = exports.formatStack = exports.parseQueryString = exports.checkWhiteScreen = exports.getSelector = exports.getLogBaseData = exports.getLastEvent = void 0;
7
+ var getLastEvent_1 = require("./getLastEvent");
8
+ Object.defineProperty(exports, "getLastEvent", { enumerable: true, get: function () { return __importDefault(getLastEvent_1).default; } });
9
+ var getLogBaseData_1 = require("./getLogBaseData");
10
+ Object.defineProperty(exports, "getLogBaseData", { enumerable: true, get: function () { return __importDefault(getLogBaseData_1).default; } });
11
+ var getSelector_1 = require("./getSelector");
12
+ Object.defineProperty(exports, "getSelector", { enumerable: true, get: function () { return __importDefault(getSelector_1).default; } });
13
+ var checkWhiteScreen_1 = require("./checkWhiteScreen");
14
+ Object.defineProperty(exports, "checkWhiteScreen", { enumerable: true, get: function () { return __importDefault(checkWhiteScreen_1).default; } });
15
+ var parseQueryString_1 = require("./parseQueryString");
16
+ Object.defineProperty(exports, "parseQueryString", { enumerable: true, get: function () { return __importDefault(parseQueryString_1).default; } });
17
+ var formatStack_1 = require("./formatStack");
18
+ Object.defineProperty(exports, "formatStack", { enumerable: true, get: function () { return __importDefault(formatStack_1).default; } });
19
+ var toExcEvent_1 = require("./toExcEvent");
20
+ Object.defineProperty(exports, "toExcEvents", { enumerable: true, get: function () { return toExcEvent_1.toExcEvents; } });
21
+ Object.defineProperty(exports, "isExceptionReportUrl", { enumerable: true, get: function () { return toExcEvent_1.isExceptionReportUrl; } });
22
+ Object.defineProperty(exports, "mapRequestLog", { enumerable: true, get: function () { return toExcEvent_1.mapRequestLog; } });
@@ -0,0 +1 @@
1
+ export default function parseQueryString(url: string): Record<string, string>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = parseQueryString;
4
+ function parseQueryString(url) {
5
+ const queryString = url.split("?")[1];
6
+ const queryParams = {};
7
+ queryString === null || queryString === void 0 ? void 0 : queryString.split("&").forEach(item => {
8
+ const [key, value] = item.split("=");
9
+ queryParams[key] = value;
10
+ });
11
+ return queryParams;
12
+ }
@@ -0,0 +1,8 @@
1
+ import type { ExcEventPayload } from '../exception/types';
2
+ import type { BaseLog, IMonitorOptions, MonitorTypeLog, RequestLog } from '../interface';
3
+ /** RequestLog(失败)→ request */
4
+ export declare function mapRequestLog(req: RequestLog, base: BaseLog, opts: Pick<IMonitorOptions, 'userId' | 'release' | 'environment'>): ExcEventPayload | null;
5
+ /** monitor 采集日志 → 异常平台事件列表 */
6
+ export declare function toExcEvents(data: MonitorTypeLog, base: BaseLog, opts: Pick<IMonitorOptions, 'userId' | 'release' | 'environment'>): ExcEventPayload[];
7
+ /** 是否为异常平台上报 URL(防递归拦截) */
8
+ export declare function isExceptionReportUrl(url: string | URL, endpoint?: string): boolean;
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapRequestLog = mapRequestLog;
4
+ exports.toExcEvents = toExcEvents;
5
+ exports.isExceptionReportUrl = isExceptionReportUrl;
6
+ /** 当前页面 pathname + search */
7
+ function currentPage() {
8
+ try {
9
+ return `${location.pathname}${location.search}`;
10
+ }
11
+ catch (_a) {
12
+ return '';
13
+ }
14
+ }
15
+ function baseDeviceFields(base) {
16
+ return {
17
+ title: base.title,
18
+ url: base.url,
19
+ browser: base.browser,
20
+ os: base.os,
21
+ deviceInfo: base.device,
22
+ userAgent: base.userAgent,
23
+ };
24
+ }
25
+ function contextFields(base, opts) {
26
+ return {
27
+ page: currentPage(),
28
+ userId: opts.userId,
29
+ release: opts.release,
30
+ environment: opts.environment,
31
+ device: baseDeviceFields(base),
32
+ };
33
+ }
34
+ function mapErrorLog(log, base, opts) {
35
+ var _a;
36
+ const ctx = contextFields(base, opts);
37
+ if (log.errorType === 'promiseError') {
38
+ return {
39
+ type: 'unhandledrejection',
40
+ level: 'ERROR',
41
+ message: log.message,
42
+ stack: log.stack,
43
+ ...ctx,
44
+ device: { ...ctx.device, selector: log.selector, filename: log.filename, position: log.position, errorType: log.errorType },
45
+ };
46
+ }
47
+ if (log.errorType === 'loadResourceError') {
48
+ const url = (_a = log.filename) !== null && _a !== void 0 ? _a : log.message;
49
+ return {
50
+ type: 'resource',
51
+ level: 'ERROR',
52
+ message: url ? `Resource failed: ${url}` : log.message,
53
+ ...ctx,
54
+ device: { ...ctx.device, tagName: log.tagName, selector: log.selector, errorType: log.errorType },
55
+ };
56
+ }
57
+ return {
58
+ type: 'error',
59
+ level: 'ERROR',
60
+ message: log.message,
61
+ stack: log.stack,
62
+ ...ctx,
63
+ device: {
64
+ ...ctx.device,
65
+ selector: log.selector,
66
+ isWhiteScreen: log.isWhiteScreen,
67
+ filename: log.filename,
68
+ position: log.position,
69
+ errorType: log.errorType,
70
+ },
71
+ };
72
+ }
73
+ function mapPaintLog(log, base, opts) {
74
+ const ctx = contextFields(base, opts);
75
+ return {
76
+ type: 'paint',
77
+ level: 'INFO',
78
+ message: 'paint metrics',
79
+ ...ctx,
80
+ device: { ...ctx.device, FP: log.FP, FCP: log.FCP, FMP: log.FMP, LCP: log.LCP },
81
+ };
82
+ }
83
+ function mapTimingLog(log, base, opts) {
84
+ const ctx = contextFields(base, opts);
85
+ return {
86
+ type: 'timing',
87
+ level: 'INFO',
88
+ message: 'timing metrics',
89
+ ...ctx,
90
+ device: { ...ctx.device, DOMContentLoadedTime: log.DOMContentLoadedTime, loadTime: log.loadTime },
91
+ };
92
+ }
93
+ function mapLongTaskLog(log, base, opts) {
94
+ const ctx = contextFields(base, opts);
95
+ return {
96
+ type: 'longTask',
97
+ level: log.duration >= 100 ? 'WARN' : 'INFO',
98
+ message: `long task ${log.duration}ms`,
99
+ ...ctx,
100
+ device: { ...ctx.device, startTime: log.startTime, duration: log.duration, selector: log.selector, eventType: log.eventType },
101
+ };
102
+ }
103
+ /** RequestLog(失败)→ request */
104
+ function mapRequestLog(req, base, opts) {
105
+ var _a, _b, _c;
106
+ if (req.success !== false)
107
+ return null;
108
+ const url = String((_a = req.url) !== null && _a !== void 0 ? _a : '');
109
+ const method = String((_b = req.method) !== null && _b !== void 0 ? _b : 'GET').toUpperCase();
110
+ const status = (_c = req.status) !== null && _c !== void 0 ? _c : 0;
111
+ const reason = status === 0 ? 'network' : String(status);
112
+ const ctx = contextFields(base, opts);
113
+ return {
114
+ type: 'request',
115
+ level: 'ERROR',
116
+ message: `Request failed [${reason}]: ${method} ${url}`,
117
+ ...ctx,
118
+ device: { ...ctx.device, durationMs: req.duration, msg: req.msg },
119
+ };
120
+ }
121
+ function mapBatchRequestLog(log, base, opts) {
122
+ return log.requests
123
+ .map(req => mapRequestLog(req, base, opts))
124
+ .filter((e) => e != null);
125
+ }
126
+ /** monitor 采集日志 → 异常平台事件列表 */
127
+ function toExcEvents(data, base, opts) {
128
+ if (data.type === 'error')
129
+ return [mapErrorLog(data, base, opts)];
130
+ if (data.type === 'paint')
131
+ return [mapPaintLog(data, base, opts)];
132
+ if (data.type === 'timing')
133
+ return [mapTimingLog(data, base, opts)];
134
+ if (data.type === 'longTask')
135
+ return [mapLongTaskLog(data, base, opts)];
136
+ if (data.type === 'batchXHR')
137
+ return mapBatchRequestLog(data, base, opts);
138
+ if (data.type === 'xhr' || data.type === 'fetch') {
139
+ const one = mapRequestLog(data, base, opts);
140
+ return one ? [one] : [];
141
+ }
142
+ return [];
143
+ }
144
+ /** 是否为异常平台上报 URL(防递归拦截) */
145
+ function isExceptionReportUrl(url, endpoint) {
146
+ if (!endpoint)
147
+ return false;
148
+ return String(url) === endpoint;
149
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@qiu_jun/monitors",
3
+ "version": "0.0.1",
4
+ "description": "前端监控,用于捕获,分析,和报告网站或应用程序中的异常,错误和性能问题的方法",
5
+ "sideEffects": false,
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/monitor.umd.js",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./h5": {
16
+ "types": "./dist/h5.d.ts",
17
+ "default": "./dist/h5.js"
18
+ },
19
+ "./reporter": {
20
+ "types": "./dist/reporter.d.ts",
21
+ "default": "./dist/reporter.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "scripts": {
28
+ "preinstall": "npx only-allow pnpm",
29
+ "build": "pnpm run clean && pnpm run compile && pnpm run bundle",
30
+ "clean": "rimraf dist",
31
+ "compile": "tsc -p tsconfig.build.json",
32
+ "bundle": "vite build"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public",
36
+ "registry": "https://registry.npmjs.org/"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/Qiu-Jun/monitor.git"
41
+ },
42
+ "author": "June",
43
+ "license": "ISC",
44
+ "bugs": {
45
+ "url": "https://github.com/Qiu-Jun/monitor/issues"
46
+ },
47
+ "homepage": "https://github.com/Qiu-Jun/monitor#readme",
48
+ "devDependencies": {
49
+ "rimraf": "^6.0.1",
50
+ "terser": "^5.43.1",
51
+ "typescript": "~6.0.3",
52
+ "vite": "^6.2.0"
53
+ },
54
+ "dependencies": {
55
+ "ua-parser-js": "^2.0.4"
56
+ }
57
+ }