npm.io
1.0.3 • Published 3d ago

@oxvo/network-instrumentation

Licence
MIT
Version
1.0.3
Deps
0
Size
92 kB
Vulns
0
Weekly
0

OxvoSessions Network Instrumentation

@oxvo/network-instrumentation is the shared proxy layer used by OxvoSessions browser tooling to observe fetch, XMLHttpRequest, and navigator.sendBeacon traffic without changing application request code.

It is intended for controlled SDK integrations where you need consistent request capture, header filtering, session-token injection, and payload sanitization before events are forwarded downstream.

Install

npm install @oxvo/network-instrumentation

Example

import createNetworkProxy from "@oxvo/network-instrumentation";

const ignoredHeaders = ["authorization", "cookie"];

const setSessionTokenHeader = (
  setRequestHeader: (name: string, value: string) => void,
) => {
  const siteSessionToken = "session-123";
  if (siteSessionToken) {
    setRequestHeader("X-Session-Token", siteSessionToken);
  }
};

const sanitize = (requestResponse: any) => {
  if (requestResponse?.request?.body) {
    delete requestResponse.request.body;
  }
  return requestResponse;
};

createNetworkProxy(
  window,
  ignoredHeaders,
  setSessionTokenHeader,
  sanitize,
  (message) => {
    console.log("network event", message);
  },
  (url) => url.includes("/internal/health"),
  {
    xhr: true,
    fetch: true,
    beacon: true,
  },
  (url) => url.includes("/api"),
);

Integration Notes

  • Pass window in browser environments and globalThis in test or non-DOM runtimes.
  • Keep ignored header names lowercase so filtering remains predictable across transport types.
  • Use sanitize to remove secrets or large bodies before forwarding captured traffic.
  • Provide a tokenUrlMatcher only when the session token header should be limited to selected endpoints.
  • If you need to disable instrumentation, restore the original fetch, XMLHttpRequest, or sendBeacon references that your application saved before proxying.

Exported API

The package exports:

  • createNetworkProxy as the default export
  • FetchProxy
  • XHRProxy
  • BeaconProxy
  • INetworkMessage
  • RequestResponseData