All files / src/handlers handleDownloadScript.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

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        6x 6x 6x   6x 6x   6x 6x 6x       6x 6x   6x      
import { addTrafficMonitoringSearchParamsForProCDN, createFallbackErrorResponse, getAgentScriptPath } from '../utils'
import { CacheOverride } from 'fastly:cache-override'
 
function makeDownloadScriptRequest(request: Request): Promise<Response> {
  const url = new URL(request.url)
  url.pathname = getAgentScriptPath(url.searchParams)
  addTrafficMonitoringSearchParamsForProCDN(url)
 
  const newRequest = new Request(url.toString(), request as RequestInit)
  newRequest.headers.delete('Cookie')
 
  console.log(`Downloading script from cdnEndpoint ${url.toString()}...`)
  const cache = new CacheOverride('override', { ttl: 60 }) // Caches sub-request by 60 seconds for agent download script
  return fetch(newRequest, { backend: 'fpcdn.io', cacheOverride: cache })
}
 
export async function handleDownloadScript(request: Request): Promise<Response> {
  try {
    return await makeDownloadScriptRequest(request)
  } catch (e) {
    return createFallbackErrorResponse(request, e)
  }
}