Skip to content

Commit 8c41efd

Browse files
committed
better patch
1 parent 0f30b29 commit 8c41efd

File tree

1 file changed

+68
-71
lines changed
  • packages/rrweb/src/plugins/network/record

1 file changed

+68
-71
lines changed

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

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function initXhrObserver(
223223
options.recordBody.response);
224224

225225
const restorePatch = patch(
226-
XMLHttpRequest.prototype,
226+
win.XMLHttpRequest.prototype,
227227
'open',
228228
(originalOpen: typeof XMLHttpRequest.prototype.open) => {
229229
return function (
@@ -349,82 +349,79 @@ function initFetchObserver(
349349
!('response' in options.recordBody) ||
350350
options.recordBody.response);
351351

352-
const originalFetch = win.fetch;
353-
const wrappedFetch: typeof fetch = async (url, init) => {
354-
const req = new Request(url, init);
355-
let res: Response | undefined;
356-
const networkRequest: Partial<NetworkRequest> = {};
357-
let after: number | undefined;
358-
let before: number | undefined;
359-
try {
360-
if (recordRequestHeaders) {
361-
networkRequest.requestHeaders = {};
362-
req.headers.forEach((value, header) => {
363-
networkRequest.requestHeaders![header] = value;
364-
});
365-
}
366-
if (recordRequestBody) {
367-
if (req.body === undefined || req.body === null) {
368-
networkRequest.requestBody = null;
369-
} else {
370-
networkRequest.requestBody = req.body;
352+
const restorePatch = patch(win, 'fetch', (originalFetch: typeof fetch) => {
353+
return async function (
354+
url: URL | RequestInfo,
355+
init?: RequestInit | undefined,
356+
) {
357+
const req = new Request(url, init);
358+
let res: Response | undefined;
359+
const networkRequest: Partial<NetworkRequest> = {};
360+
let after: number | undefined;
361+
let before: number | undefined;
362+
try {
363+
if (recordRequestHeaders) {
364+
networkRequest.requestHeaders = {};
365+
req.headers.forEach((value, header) => {
366+
networkRequest.requestHeaders![header] = value;
367+
});
371368
}
372-
}
373-
after = win.performance.now();
374-
res = await originalFetch(req);
375-
before = win.performance.now();
376-
if (recordResponseHeaders) {
377-
networkRequest.responseHeaders = {};
378-
res.headers.forEach((value, header) => {
379-
networkRequest.responseHeaders![header] = value;
380-
});
381-
}
382-
if (recordResponseBody) {
383-
let body: string | undefined;
384-
try {
385-
body = await res.clone().text();
386-
} catch {
387-
//
369+
if (recordRequestBody) {
370+
if (req.body === undefined || req.body === null) {
371+
networkRequest.requestBody = null;
372+
} else {
373+
networkRequest.requestBody = req.body;
374+
}
375+
}
376+
after = win.performance.now();
377+
res = await originalFetch(req);
378+
before = win.performance.now();
379+
if (recordResponseHeaders) {
380+
networkRequest.responseHeaders = {};
381+
res.headers.forEach((value, header) => {
382+
networkRequest.responseHeaders![header] = value;
383+
});
388384
}
389-
if (res.body === undefined || res.body === null) {
390-
networkRequest.responseBody = null;
391-
} else {
392-
networkRequest.responseBody = body;
385+
if (recordResponseBody) {
386+
let body: string | undefined;
387+
try {
388+
body = await res.clone().text();
389+
} catch {
390+
//
391+
}
392+
if (res.body === undefined || res.body === null) {
393+
networkRequest.responseBody = null;
394+
} else {
395+
networkRequest.responseBody = body;
396+
}
393397
}
398+
return res;
399+
} finally {
400+
getRequestPerformanceEntry(win, 'fetch', req.url, after, before)
401+
.then((entry) => {
402+
const request: NetworkRequest = {
403+
url: entry.name,
404+
method: req.method,
405+
initiatorType: entry.initiatorType as InitiatorType,
406+
status: res?.status,
407+
startTime: Math.round(entry.startTime),
408+
endTime: Math.round(entry.responseEnd),
409+
requestHeaders: networkRequest.requestHeaders,
410+
requestBody: networkRequest.requestBody,
411+
responseHeaders: networkRequest.responseHeaders,
412+
responseBody: networkRequest.responseBody,
413+
};
414+
cb({ requests: [request] });
415+
})
416+
.catch(() => {
417+
//
418+
});
394419
}
395-
return res;
396-
} finally {
397-
getRequestPerformanceEntry(win, 'fetch', req.url, after, before)
398-
.then((entry) => {
399-
const request: NetworkRequest = {
400-
url: entry.name,
401-
method: req.method,
402-
initiatorType: entry.initiatorType as InitiatorType,
403-
status: res?.status,
404-
startTime: Math.round(entry.startTime),
405-
endTime: Math.round(entry.responseEnd),
406-
requestHeaders: networkRequest.requestHeaders,
407-
requestBody: networkRequest.requestBody,
408-
responseHeaders: networkRequest.responseHeaders,
409-
responseBody: networkRequest.responseBody,
410-
};
411-
cb({ requests: [request] });
412-
})
413-
.catch(() => {
414-
//
415-
});
416-
}
417-
};
418-
wrappedFetch.prototype = {};
419-
Object.defineProperties(wrappedFetch, {
420-
__rrweb_original__: {
421-
enumerable: false,
422-
value: originalFetch,
423-
},
420+
};
424421
});
425-
win.fetch = wrappedFetch;
422+
426423
return () => {
427-
win.fetch = originalFetch;
424+
restorePatch();
428425
};
429426
}
430427

0 commit comments

Comments
 (0)