network layer: change beacon creation, publish .4

This commit is contained in:
nick-delirium 2024-11-15 10:14:49 +01:00
parent 1b748293d5
commit 1bf697e5d9
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
3 changed files with 7 additions and 10 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@openreplay/network-proxy",
"version": "1.0.3",
"version": "1.0.4",
"description": "this library helps us to create proxy objects for fetch, XHR and beacons for proper request tracking.",
"main": "dist/index.js",
"module": "dist/index.js",

View file

@ -1,5 +1,5 @@
import NetworkMessage from './networkMessage'
import { RequestState, INetworkMessage, RequestResponseData } from './types';
import { INetworkMessage, RequestResponseData } from './types';
import { genStringBody, getURL } from './utils'
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
@ -74,24 +74,19 @@ export class BeaconProxyHandler<T extends typeof navigator.sendBeacon> implement
}
export default class BeaconProxy {
public static origSendBeacon = typeof window !== 'undefined' ? window.navigator?.sendBeacon : undefined
public static hasSendBeacon() {
return !!BeaconProxy.origSendBeacon
}
public static create(
originalSendBeacon: typeof window.navigator.sendBeacon,
ignoredHeaders: boolean | string[],
setSessionTokenHeader: (cb: (name: string, value: string) => void) => void,
sanitize: (data: RequestResponseData) => RequestResponseData | null,
sendMessage: (item: INetworkMessage) => void,
isServiceUrl: (url: string) => boolean,
) {
if (!BeaconProxy.hasSendBeacon()) {
if (!originalSendBeacon) {
return undefined
}
return new Proxy(
BeaconProxy.origSendBeacon,
originalSendBeacon,
new BeaconProxyHandler(
ignoredHeaders,
setSessionTokenHeader,

View file

@ -85,7 +85,9 @@ export default function createNetworkProxy(
}
if (modules.beacon) {
if (context.navigator?.sendBeacon) {
const origBeacon = context.navigator.sendBeacon
context.navigator.sendBeacon = BeaconProxy.create(
origBeacon,
ignoredHeaders,
setSessionTokenHeader,
sanitize,