UNPKG

whitch-frontend-monitor

Version:

前端监控系统 - 一个轻量级的前端监控SDK

198 lines 9.83 kB
"use strict"; /** * 网络请求监控模块 */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NetworkMonitor = void 0; var reporter_1 = require("../reporter"); var NetworkMonitor = /** @class */ (function () { function NetworkMonitor() { } /** * 初始化网络请求监控 */ NetworkMonitor.init = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; // 获取已初始化的Reporter实例 var reportConfig = { reportUrl: ((_b = (_a = window.TraeMonitor) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.reportUrl) || '', delay: ((_d = (_c = window.TraeMonitor) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.delay) || 1000, maxRetry: ((_f = (_e = window.TraeMonitor) === null || _e === void 0 ? void 0 : _e.config) === null || _f === void 0 ? void 0 : _f.maxRetry) || 3, sampleRate: ((_h = (_g = window.TraeMonitor) === null || _g === void 0 ? void 0 : _g.config) === null || _h === void 0 ? void 0 : _h.sampleRate) || 1, ignoreUrls: ((_k = (_j = window.TraeMonitor) === null || _j === void 0 ? void 0 : _j.config) === null || _k === void 0 ? void 0 : _k.ignoreUrls) || [], compression: ((_m = (_l = window.TraeMonitor) === null || _l === void 0 ? void 0 : _l.config) === null || _m === void 0 ? void 0 : _m.compression) || false }; if (!reportConfig.reportUrl) { console.warn('Reporter URL is not configured'); return; } this.reporter = reporter_1.Reporter.getInstance(reportConfig); this.initXHRMonitor(); this.initFetchMonitor(); }; /** * 监控XMLHttpRequest请求 */ NetworkMonitor.initXHRMonitor = function () { var originalXHR = window.XMLHttpRequest; window.XMLHttpRequest = function () { var xhr = new originalXHR(); var startTime = Date.now(); var method = ''; var url = ''; xhr.addEventListener('loadstart', function () { method = xhr._method || 'GET'; url = xhr._url || ''; }); xhr.addEventListener('loadend', function () { var duration = Date.now() - startTime; var networkInfo = { type: 'xhr', method: method, url: url, status: xhr.status, duration: duration, success: xhr.status >= 200 && xhr.status < 300, timestamp: startTime }; if (!networkInfo.success) { networkInfo.error = "HTTP Error: ".concat(xhr.status, " ").concat(xhr.statusText); } NetworkMonitor.networkList.push(networkInfo); // 添加到上报队列 NetworkMonitor.reporter.addToQueue({ type: 'network', data: networkInfo, timestamp: Date.now() }); }); var originalOpen = xhr.open; xhr.open = function (method, url) { xhr._method = method; xhr._url = url; originalOpen.apply(xhr, arguments); }; return xhr; }; }; /** * 监控Fetch请求 */ NetworkMonitor.initFetchMonitor = function () { var originalFetch = window.fetch; window.fetch = function (input, init) { return __awaiter(this, void 0, void 0, function () { var startTime, method, url, response, duration, networkInfo, error_1, duration, networkInfo; return __generator(this, function (_a) { switch (_a.label) { case 0: startTime = Date.now(); method = ((init === null || init === void 0 ? void 0 : init.method) || 'GET').toUpperCase(); url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, originalFetch(input, init)]; case 2: response = _a.sent(); duration = Date.now() - startTime; networkInfo = { type: 'fetch', method: method, url: url, status: response.status, duration: duration, success: response.ok, timestamp: startTime }; if (!networkInfo.success) { networkInfo.error = "HTTP Error: ".concat(response.status, " ").concat(response.statusText); } NetworkMonitor.networkList.push(networkInfo); // 添加到上报队列 NetworkMonitor.reporter.addToQueue({ type: 'network', data: networkInfo, timestamp: Date.now() }); return [2 /*return*/, response]; case 3: error_1 = _a.sent(); duration = Date.now() - startTime; networkInfo = { type: 'fetch', method: method, url: url, status: 0, duration: duration, success: false, error: error_1.message, timestamp: startTime }; NetworkMonitor.networkList.push(networkInfo); // 添加到上报队列 NetworkMonitor.reporter.addToQueue({ type: 'network', data: networkInfo, timestamp: Date.now() }); throw error_1; case 4: return [2 /*return*/]; } }); }); }; }; /** * 获取网络请求监控数据 */ NetworkMonitor.getNetworkData = function () { return this.networkList; }; /** * 清空网络请求数据 */ NetworkMonitor.clearNetworkData = function () { this.networkList = []; }; NetworkMonitor.networkList = []; return NetworkMonitor; }()); exports.NetworkMonitor = NetworkMonitor; //# sourceMappingURL=network.js.map