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)

graph LR startNetworkMonitoring["startNetworkMonitoring"] onNetworkRequestStart["onNetworkRequestStart"] onNetworkRequestEnd["onNetworkRequestEnd"] startNetworkMonitoring -->|calls| onNetworkRequestStart startNetworkMonitoring -->|calls| onNetworkRequestEnd style startNetworkMonitoring fill:#dbeafe,stroke:#2563eb,stroke-width:2px click startNetworkMonitoring "89e83cdb6693637a.html" click onNetworkRequestStart "8a7bd3ca79b8eff1.html" click onNetworkRequestEnd "1f21a1de3c97b1ca.html"
TargetType
onNetworkRequestStart calls
onNetworkRequestEnd calls
loadend dynamic_call

Impact (Incoming)

graph LR startNetworkMonitoring["startNetworkMonitoring"] startRecording["startRecording"] tryRestoreRecordingSession["tryRestoreRecordingSession"] startRecording -->|calls| startNetworkMonitoring tryRestoreRecordingSession -->|calls| startNetworkMonitoring style startNetworkMonitoring fill:#dbeafe,stroke:#2563eb,stroke-width:2px click startNetworkMonitoring "89e83cdb6693637a.html" click startRecording "5a5a69de77cdf6a4.html" click tryRestoreRecordingSession "38d7b2802373484d.html"