All files / worker/utils origin.ts

65.21% Statements 15/23
87.5% Branches 7/8
100% Functions 3/3
65.21% Lines 15/23

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                    55x   55x                                   55x                                   15x 2x       13x 4x   9x       13x                                   45x 28x     17x 17x   17x             17x    
import { Route } from '@fingerprintjs/url-matcher'
import { TypedEnv } from '../types'
import { IdentificationClient } from '../fingerprint/identificationClient'
import { setEdgeResponseHeaders } from './edgeHeaders'
import { isEdgeApiEnabled } from '../env'
import { copyRequest } from './request'
import { EdgeResponse } from '../fingerprint/identificationClientTypes'
import { shouldSkipEdgeRequest } from './edgeFilter'
 
export function fetchOrigin(request: Request) {
  const origin = import.meta.env.VITE_ORIGIN
 
  Iif (origin) {
    const originUrl = new URL(origin)
    const requestUrl = new URL(request.url)
 
    originUrl.pathname = requestUrl.pathname
    originUrl.search = requestUrl.search
    const headers = new Headers(request.headers)
    headers.set('Host', originUrl.host)
 
    console.log(`Using local override: ${originUrl}`)
    return fetch(originUrl, {
      //duplex: 'half', // The CF types don't support duplex right now but wrangler needs it locally
      headers,
      method: request.method,
      body: request.body,
    })
  }
 
  return fetch(request)
}
 
/**
 * Fetches the origin with the Edge API headers if enabled, otherwise directly fetches the origin.
 *
 * @param {Request} request - The incoming HTTP request to be processed.
 * @param {IdentificationClient} identificationClient - The client responsible for interacting with the Edge API.
 * @param {TypedEnv} env - The environment configuration object, used to determine if the Edge API is enabled.
 * @param {Route} route - The identification page route that matched the request.
 * @return {Promise<Response>} A promise that resolves to the HTTP response from the origin.
 */
export async function fetchOriginWithEdgeAPIRequest(
  request: Request,
  identificationClient: IdentificationClient,
  env: TypedEnv,
  route: Route<unknown>
): Promise<Response> {
  if (!isEdgeApiEnabled(env)) {
    return fetchOrigin(request)
  }
 
  let edgeResponse: EdgeResponse | undefined
  if (shouldSkipEdgeRequest(request, env, route)) {
    console.debug('Skipping Edge API request for static asset:', request.url)
  } else {
    edgeResponse = await identificationClient.safeEdge(request)
  }
 
  // client-supplied (spoofed) Edge headers are stripped either way here
  return fetchOriginWithEdgeAPIHeaders(request, env, edgeResponse)
}
 
/**
 * Fetches the origin resource with custom headers from the Edge API
 * if the Edge API is enabled. Merges the original request headers
 * with the headers generated from the Edge API response.
 *
 * @param {Request} request - The original request object.
 * @param {TypedEnv} env - The environment settings, determines if Edge API is enabled.
 * @param {EdgeResponse} [edgeResponse] - Optional response from the Edge API, used to create additional headers.
 * @return {Promise<Response>} A promise that resolves to the response from the origin server.
 */
export async function fetchOriginWithEdgeAPIHeaders(
  request: Request,
  env: TypedEnv,
  edgeResponse?: EdgeResponse
): Promise<Response> {
  if (!isEdgeApiEnabled(env)) {
    return fetchOrigin(request)
  }
 
  const originRequestHeaders = new Headers(request.headers)
  setEdgeResponseHeaders(originRequestHeaders, edgeResponse)
 
  const originRequest = copyRequest({
    request,
    init: {
      headers: originRequestHeaders,
    },
  })
 
  return fetchOrigin(originRequest)
}