All files / src/worker/fingerprint agent.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 1/1
100% Lines 10/10

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 43 44 45 46 47        4x                               4x 4x 4x   4x   4x 4x   4x               4x   4x              
import { createResponseWithMaxAge, fetchCacheable } from '../utils/cache'
import { copyRequest } from '../utils/request'
import { addIntegrationInfo } from '../../shared/integrationInfo'
 
const workerTtl = 60
 
/**
 * Fetches and returns a script with FingerprintJS Pro agent loader for the specified public API key.
 * Note: The fetched script is cached using the `fetchCacheable` function.
 *
 * @param {Request} incomingRequest - The incoming HTTP request received by the worker.
 * @param {string} publicApiKey - The public API key used to generate the script URL.
 * @param {string} cdnHost - Hostname of the Fingerprint CDN.
 * @return {Promise<Response>} A promise that resolves to a Response object containing the fetched script with the 'Content-Type' header set to 'application/javascript'.
 */
export async function getAgentLoader(
  incomingRequest: Request,
  publicApiKey: string,
  cdnHost: string
): Promise<Response> {
  const fpScriptUrl = new URL(`https://${cdnHost}/v4/${publicApiKey}`)
  fpScriptUrl.search = new URL(incomingRequest.url).search
  addIntegrationInfo(fpScriptUrl, 'procdn')
 
  console.debug('Fetching agent loader from:', fpScriptUrl)
 
  const requestHeaders = new Headers(incomingRequest.headers)
  requestHeaders.delete('Cookie')
 
  const request = copyRequest({
    request: incomingRequest,
    url: fpScriptUrl,
    init: {
      headers: requestHeaders,
    },
  })
 
  const response = await fetchCacheable(request, workerTtl)
 
  return createResponseWithMaxAge(response, {
    // Cache in browser for up to 1 hour
    maxAge: 3600,
    // Cache in CDN for up to 1 minute
    sMaxAge: 60,
  })
}