All files / src/utils addProxyIntegrationHeaders.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 2/2
100% Lines 12/12

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 2312x   12x 35x 35x 33x     35x 35x     12x 40x     40x 31x     9x    
import { getProxySecret, WorkerEnv } from '../env'
 
export function addProxyIntegrationHeaders(headers: Headers, url: string, env: WorkerEnv) {
  const proxySecret = getProxySecret(env)
  if (proxySecret) {
    headers.set('FPJS-Proxy-Secret', proxySecret)
  }
 
  headers.set('FPJS-Proxy-Client-IP', getIPFromHeaders(headers))
  headers.set('FPJS-Proxy-Forwarded-Host', new URL(url).hostname)
}
 
export function getIPFromHeaders(headers: Headers) {
  const connectingIP = headers.get('CF-Connecting-IP')
 
  // Correctly handle CloudFlare's [Pseudo IPv4](https://developers.cloudflare.com/network/pseudo-ipv4/) feature set to `Overwrite headers`
  if (headers.get('Cf-Pseudo-IPv4') === connectingIP) {
    return headers.get('CF-Connecting-IPv6') || ''
  }
 
  return connectingIP || ''
}