|
| 1 | +import { TraceSubTypeEnum, TraceTypeEnum } from '../../../common/enum'; |
| 2 | +import { lazyReportBatch } from '../../../common/report'; |
| 3 | +import { urlToJson } from '../../../common/utils'; |
| 4 | +import { AjaxType } from '../../../types'; |
| 5 | + |
1 | 6 | export const originalProto = XMLHttpRequest.prototype; |
2 | | -export const originalOpen = XMLHttpRequest.prototype.open; |
3 | | -export const originalSend = XMLHttpRequest.prototype.send; |
| 7 | +export const originalSend = originalProto.send; |
| 8 | +export const originalOpen = originalProto.open; |
4 | 9 |
|
5 | | -// Map to store metadata for each XMLHttpRequest instance |
6 | | -const xhrMetadata = new WeakMap< |
7 | | - XMLHttpRequest, |
8 | | - { |
9 | | - url?: string | URL; |
10 | | - method?: string; |
| 10 | +// 扩展 XMLHttpRequest 类型,允许自定义属性 |
| 11 | +declare global { |
| 12 | + interface XMLHttpRequest { |
11 | 13 | startTime?: number; |
12 | 14 | endTime?: number; |
13 | 15 | duration?: number; |
14 | | - status?: number; |
| 16 | + method?: string; |
| 17 | + url?: string; |
15 | 18 | } |
16 | | ->(); |
| 19 | +} |
17 | 20 |
|
18 | 21 | function overwriteOpenAndSend() { |
19 | | - originalProto.open = function ( |
20 | | - this: XMLHttpRequest, |
| 22 | + originalProto.open = function newOpen( |
21 | 23 | method: string, |
22 | 24 | url: string | URL, |
23 | | - async?: boolean, |
24 | | - username?: string | null, |
25 | | - password?: string | null, |
| 25 | + async: boolean = true, |
| 26 | + username?: string, |
| 27 | + password?: string, |
26 | 28 | ) { |
27 | | - // Store metadata in the WeakMap |
28 | | - xhrMetadata.set(this, { url, method }); |
29 | | - (originalOpen as any).call(this, method, url, async, username, password); |
| 29 | + // 这将保留原始的 open 方法签名,并确保 async、username 和 password 可选 |
| 30 | + this.url = url.toString(); // 可能需要转为 string 类型 |
| 31 | + this.method = method; |
| 32 | + originalOpen.apply(this, [method, url, async, username, password]); |
30 | 33 | }; |
31 | 34 |
|
32 | | - originalProto.send = function (...args) { |
33 | | - const startTime = performance.now(); |
34 | | - const xhr = this as XMLHttpRequest; |
35 | | - |
36 | | - // Update startTime in the metadata |
37 | | - const metadata = xhrMetadata.get(xhr) || {}; |
38 | | - metadata.startTime = startTime; |
39 | | - xhrMetadata.set(xhr, metadata); |
40 | | - |
41 | | - const onloadEnd = () => { |
42 | | - const endTime = performance.now(); |
43 | | - const duration = endTime - (metadata.startTime || 0); |
44 | | - const status = xhr.status; |
| 35 | + originalProto.send = function newSend( |
| 36 | + ...args: [Document | XMLHttpRequestBodyInit | null | undefined] |
| 37 | + ) { |
| 38 | + this.addEventListener('loadstart', () => { |
| 39 | + this.startTime = Date.now(); |
| 40 | + }); |
45 | 41 |
|
46 | | - // Update metadata with endTime, duration, and status |
47 | | - Object.assign(metadata, { endTime, duration, status }); |
| 42 | + const onLoaded = () => { |
| 43 | + this.endTime = Date.now(); |
| 44 | + this.duration = (this.endTime ?? 0) - (this.startTime ?? 0); |
| 45 | + const { url, method, startTime, endTime, duration, status } = this; |
| 46 | + const params = (args[0] ? args[0] : urlToJson(url as string)) as string; |
48 | 47 |
|
49 | | - const reportData = { |
50 | | - url: metadata.url, |
51 | | - method: metadata.method, |
52 | | - endTime: metadata.endTime, |
53 | | - duration: metadata.duration, |
54 | | - status: metadata.status, |
55 | | - type: 'performance', |
56 | | - success: (status >= 200 && status < 300) || status === 304, |
57 | | - subType: 'xhr', |
| 48 | + const reportData: AjaxType = { |
| 49 | + status, |
| 50 | + duration, |
| 51 | + startTime, |
| 52 | + endTime, |
| 53 | + url, |
| 54 | + method: method?.toUpperCase(), |
| 55 | + type: TraceTypeEnum.performance, |
| 56 | + success: status >= 200 && status < 300, |
| 57 | + subType: TraceSubTypeEnum.xhr, |
| 58 | + pageUrl: window.location.href, |
| 59 | + params, |
| 60 | + timestamp: new Date().getTime(), |
58 | 61 | }; |
59 | | - |
60 | | - // Remove the event listener to avoid memory leaks |
61 | | - xhr.removeEventListener('loadend', onloadEnd, true); |
62 | | - |
63 | | - // You can now use `reportData` for reporting purposes |
64 | | - console.log(reportData); // Example: Replace with actual reporting logic |
| 62 | + // todo: 发送数据 |
| 63 | + lazyReportBatch(reportData); |
| 64 | + this.removeEventListener('loadend', onLoaded, true); |
65 | 65 | }; |
66 | 66 |
|
67 | | - xhr.addEventListener('loadend', onloadEnd, true); |
68 | | - (originalSend as any).call(xhr, ...args); |
| 67 | + this.addEventListener('loadend', onLoaded, true); |
| 68 | + originalSend.apply(this, args); |
69 | 69 | }; |
70 | 70 | } |
71 | 71 |
|
|
0 commit comments