All files / src/scripts/instrumentor instrumentor.ts

75% Statements 6/8
50% Branches 1/2
100% Functions 1/1
75% Lines 6/8

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 31 32 33 34 35 36 37 38 39 40 41 42                                                4x         4x   4x           4x 4x 4x    
import { WritablePatcherContext } from './patcher/context'
import { patchFetch } from './patcher/fetch/fetch'
import { setupPatcherContext } from './fingerprint/patcherContext'
import { FingerprintLoader } from '../shared/fingerprint/types'
import { ProtectedApi } from '../../shared/types'
import { patchXHR } from './patcher/xhr/xhr'
import { patchForms } from './patcher/form/form'
import { logger } from '../shared/logger'
 
export type InstrumentationParams = {
  fingerprintLoader: Promise<FingerprintLoader>
  protectedApis: ProtectedApi[]
  endpoint: string
}
 
/**
 * Sets up the complete instrumentation system for signals collection, request and form patching.
 *
 * This function initializes the entire instrumentation pipeline by setting up signals
 * collection, retrieving protected APIs configuration, and patching the requests to
 * automatically add security signals to requests targeting protected endpoints.
 *
 */
export async function setupInstrumentor({ fingerprintLoader, protectedApis, endpoint }: InstrumentationParams) {
  Iif (!protectedApis.length) {
    logger.info('No protected APIs configured, skipping instrumentation.')
    return
  }
 
  const patcherCtx = new WritablePatcherContext(protectedApis)
 
  await setupPatcherContext({
    patcherCtx: patcherCtx,
    fingerprintLoader,
    endpoint,
  })
 
  patchForms(patcherCtx)
  patchFetch(patcherCtx)
  patchXHR(patcherCtx)
}