Skip to content

Commit 15ecfcf

Browse files
committed
slim NetworkRequest
1 parent dba49bf commit 15ecfcf

File tree

1 file changed

+47
-12
lines changed
  • packages/rrweb/src/plugins/network/record

1 file changed

+47
-12
lines changed

packages/rrweb/src/plugins/network/record/index.ts

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ const defaultNetworkOptions: NetworkRecordOptions = {
7878
type Headers = Record<string, string>;
7979

8080
type NetworkRequest = {
81-
performanceEntry: PerformanceEntry;
81+
requestUrl: string;
8282
requestMethod: string;
83+
requestInitiator: InitiatorType;
8384
requestHeaders?: Headers;
8485
requestBody?: string | null;
8586
responseStatus?: number;
87+
responseDuration: number;
8688
responseHeaders?: Headers;
8789
responseBody?: string | null;
8890
};
@@ -101,6 +103,13 @@ const isResourceTiming = (
101103
entry: PerformanceEntry,
102104
): entry is PerformanceResourceTiming => entry.entryType === 'resource';
103105

106+
type ObservedPerformanceEntry = (
107+
| PerformanceNavigationTiming
108+
| PerformanceResourceTiming
109+
) & {
110+
responseStatus?: number;
111+
};
112+
104113
function initPerformanceObserver(
105114
cb: networkCallback,
106115
win: IWindow,
@@ -110,7 +119,7 @@ function initPerformanceObserver(
110119
const initialPerformanceEntries = win.performance
111120
.getEntries()
112121
.filter(
113-
(entry) =>
122+
(entry): entry is ObservedPerformanceEntry =>
114123
isNavigationTiming(entry) ||
115124
(isResourceTiming(entry) &&
116125
options.initiatorTypes.includes(
@@ -119,8 +128,14 @@ function initPerformanceObserver(
119128
);
120129
cb({
121130
requests: initialPerformanceEntries.map((performanceEntry) => ({
122-
performanceEntry,
131+
requestUrl: performanceEntry.name,
123132
requestMethod: 'GET',
133+
requestInitiator: performanceEntry.initiatorType as InitiatorType,
134+
responseStatus:
135+
'responseStatus' in performanceEntry
136+
? performanceEntry.responseStatus
137+
: undefined,
138+
responseDuration: Math.round(performanceEntry.duration),
124139
})),
125140
isInitial: true,
126141
});
@@ -129,7 +144,7 @@ function initPerformanceObserver(
129144
const performanceEntries = entries
130145
.getEntries()
131146
.filter(
132-
(entry) =>
147+
(entry): entry is ObservedPerformanceEntry =>
133148
isNavigationTiming(entry) ||
134149
(isResourceTiming(entry) &&
135150
options.initiatorTypes.includes(
@@ -140,8 +155,14 @@ function initPerformanceObserver(
140155
);
141156
cb({
142157
requests: performanceEntries.map((performanceEntry) => ({
143-
performanceEntry,
158+
requestUrl: performanceEntry.name,
144159
requestMethod: 'GET',
160+
requestInitiator: performanceEntry.initiatorType as InitiatorType,
161+
responseStatus:
162+
'responseStatus' in performanceEntry
163+
? performanceEntry.responseStatus
164+
: undefined,
165+
responseDuration: Math.round(performanceEntry.duration),
145166
})),
146167
});
147168
});
@@ -158,11 +179,13 @@ const getRequestPerformanceEntry = async (
158179
after?: number,
159180
before?: number,
160181
attempt = 0,
161-
): Promise<PerformanceEntry> => {
182+
): Promise<PerformanceResourceTiming> => {
162183
if (attempt > 10) {
163184
throw new Error('Cannot find performance entry');
164185
}
165-
const urlPerformanceEntries = win.performance.getEntriesByName(url);
186+
const urlPerformanceEntries = win.performance.getEntriesByName(
187+
url,
188+
) as PerformanceResourceTiming[];
166189
const performanceEntry = findLast(
167190
urlPerformanceEntries,
168191
(performanceEntry) =>
@@ -301,10 +324,15 @@ function initXhrObserver(
301324
)
302325
.then((performanceEntry) => {
303326
const request: NetworkRequest = {
304-
performanceEntry,
327+
requestUrl: performanceEntry.name,
305328
requestMethod: req.method,
329+
requestInitiator: performanceEntry.initiatorType as InitiatorType,
330+
requestHeaders: networkRequest.requestHeaders,
331+
requestBody: networkRequest.requestBody,
306332
responseStatus: xhr.status,
307-
...networkRequest,
333+
responseDuration: performanceEntry.duration,
334+
responseHeaders: networkRequest.responseHeaders,
335+
responseBody: networkRequest.responseBody,
308336
};
309337
cb({ requests: [request] });
310338
})
@@ -355,6 +383,7 @@ function initFetchObserver(
355383
const originalFetch = win.fetch;
356384
const wrappedFetch: typeof fetch = async (url, init) => {
357385
const req = new Request(url, init);
386+
let res: Response | undefined;
358387
const networkRequest: Partial<NetworkRequest> = {};
359388
let after: number | undefined;
360389
let before: number | undefined;
@@ -378,7 +407,7 @@ function initFetchObserver(
378407
}
379408
}
380409
after = win.performance.now();
381-
const res = await originalFetch(req);
410+
res = await originalFetch(req);
382411
before = win.performance.now();
383412
networkRequest.responseStatus = res.status;
384413
if (recordResponseHeaders) {
@@ -410,9 +439,15 @@ function initFetchObserver(
410439
getRequestPerformanceEntry(win, 'fetch', req.url, after, before)
411440
.then((performanceEntry) => {
412441
const request: NetworkRequest = {
413-
performanceEntry,
442+
requestUrl: performanceEntry.name,
414443
requestMethod: req.method,
415-
...networkRequest,
444+
requestInitiator: performanceEntry.initiatorType as InitiatorType,
445+
requestHeaders: networkRequest.requestHeaders,
446+
requestBody: networkRequest.requestBody,
447+
responseStatus: res?.status,
448+
responseDuration: performanceEntry.duration,
449+
responseHeaders: networkRequest.responseHeaders,
450+
responseBody: networkRequest.responseBody,
416451
};
417452
cb({ requests: [request] });
418453
})

0 commit comments

Comments
 (0)