All files / worker/utils origin.ts

61.9% Statements 13/21
83.33% Branches 5/6
100% Functions 3/3
61.9% Lines 13/21

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                46x   46x                                   46x                               6x 2x     4x   4x                                   36x 28x     8x 8x   8x             8x    
import { TypedEnv } from '../types'
import { IdentificationClient } from '../fingerprint/identificationClient'
import { setEdgeResponseHeaders } from './headers'
import { isEdgeApiEnabled } from '../env'
import { copyRequest } from './request'
import { EdgeResponse } from '../fingerprint/identificationClientTypes'
 
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.
 * @return {Promise<Response>} A promise that resolves to the HTTP response from the origin.
 */
export async function fetchOriginWithEdgeAPIRequest(
  request: Request,
  identificationClient: IdentificationClient,
  env: TypedEnv
): Promise<Response> {
  if (!isEdgeApiEnabled(env)) {
    return fetchOrigin(request)
  }
 
  const edgeResponse = await identificationClient.safeEdge(request)
 
  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)
}