startNetworkMonitoring function ✓ 93.1%
Last updated: 2026-03-05T11:49:57.418Z
Metrics
LOC: 69
Complexity: 9
Params: 0
Coverage: 93.1% (27/29 lines, 0x executed)
Signature
startNetworkMonitoring(): : void
Source Code
function startNetworkMonitoring(): void {
// --- Intercept fetch ---
origFetch = globalThis.fetch;
globalThis.fetch = function patchedFetch(
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> {
if (!session || session.status !== "recording") {
return origFetch!.call(globalThis, input, init);
}
const method = init?.method?.toUpperCase() ?? "GET";
const url =
typeof input === "string"
? input
: input instanceof URL
? input.href
: input.url;
onNetworkRequestStart();
return origFetch!.call(globalThis, input, init).then(
(response) => {
onNetworkRequestEnd(url, method, response.status);
return response;
},
(err) => {
onNetworkRequestEnd(url, method, 0);
throw err;
},
);
};
// --- Intercept XMLHttpRequest ---
origXhrOpen = XMLHttpRequest.prototype.open;
origXhrSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (
this: XMLHttpRequest,
method: string,
url: string | URL,
...rest: unknown[]
) {
// Store per-instance to avoid race conditions between concurrent XHRs
xhrRequestInfo.set(this, {
method: method.toUpperCase(),
url: typeof url === "string" ? url : url.href,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (origXhrOpen as any).apply(this, [method, url, ...rest]);
} as typeof XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.send = function (
...args: Parameters<XMLHttpRequest["send"]>
) {
if (session?.status === "recording") {
const info = xhrRequestInfo.get(this);
if (info) {
const { method: capturedMethod, url: capturedUrl } = info;
onNetworkRequestStart();
this.addEventListener("loadend", () => {
onNetworkRequestEnd(capturedUrl, capturedMethod, this.status);
xhrRequestInfo.delete(this);
});
}
}
return origXhrSend!.apply(this, args);
};
}
Dependencies (Outgoing)
| Target | Type |
|---|---|
| onNetworkRequestStart | calls |
| onNetworkRequestEnd | calls |
| loadend | dynamic_call |
Impact (Incoming)
| Source | Type |
|---|---|
| startRecording | calls |
| tryRestoreRecordingSession | calls |