All files / scripts/instrumentor/patcher/xhr patcherRequest.ts

85.71% Statements 6/7
100% Branches 0/0
100% Functions 3/3
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                        18x       12x 12x 12x     12x 12x              
import { XHRFingerprintMetadata } from './types'
import { PatcherRequest } from '../types'
import { logger } from '../../../shared/logger'
 
/**
 * Creates a PatcherRequest object from an XMLHttpRequest and its metadata.
 *
 * @param {XMLHttpRequest} request - The XMLHttpRequest instance to configure.
 * @param {XHRFingerprintMetadata} metadata - The metadata containing URL and method for the request.
 * @return {PatcherRequest} A PatcherRequest object with URL, method, and header-setting capability.
 */
export function createPatcherRequest(request: XMLHttpRequest, metadata: XHRFingerprintMetadata): PatcherRequest {
  return {
    url: metadata.url,
    method: metadata.method,
    setIncludeCredentials() {
      const appIncludedCredentials = request.withCredentials
      request.withCredentials = true
      return appIncludedCredentials
    },
    setHeader(name: string, value: string) {
      try {
        request.setRequestHeader(name, value)
      } catch (e) {
        logger.warn('Failed to set XHR request header:', e)
      }
    },
  }
}