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 25 26          6x 6x     6x 6x   6x 6x 6x 6x       6x 6x   6x      
import { createFallbackErrorResponse, getAgentScriptPath } from '../utils'
import { CacheOverride } from 'fastly:cache-override'
import { getIngressBackendByRegion } from '../utils/getIngressBackendByRegion'
 
function makeDownloadScriptRequest(request: Request): Promise<Response> {
  const url = new URL(request.url)
  url.pathname = getAgentScriptPath(url.searchParams)
 
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fastly Request/RequestInit type mismatch with exactOptionalPropertyTypes
  const newRequest = new Request(url.toString(), request as RequestInit)
  newRequest.headers.delete('Cookie')
 
  const backend = getIngressBackendByRegion(url)
  console.log(`Downloading script from ${backend} ${url.toString()}...`)
  const cache = new CacheOverride('override', { ttl: 60 })
  return fetch(newRequest, { backend, cacheOverride: cache })
}
 
export async function handleDownloadScript(request: Request): Promise<Response> {
  try {
    return await makeDownloadScriptRequest(request)
  } catch (e) {
    return createFallbackErrorResponse(request, e)
  }
}