( context: ExecutionContext, origin: string, packageName: string, version: string, filename: string )
| 18 | } |
| 19 | |
| 20 | export async function fetchFile( |
| 21 | context: ExecutionContext, |
| 22 | origin: string, |
| 23 | packageName: string, |
| 24 | version: string, |
| 25 | filename: string |
| 26 | ): Promise<Response | null> { |
| 27 | if (filename === "" || filename === "/") { |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | let url = new URL(`/file/${packageName.toLowerCase()}@${version}${filename}`, origin); |
| 32 | let request = new Request(url); |
| 33 | |
| 34 | let cache = await caches.open("npm-files"); |
| 35 | let response = await cache.match(request); |
| 36 | if (!response) { |
| 37 | response = await fetch(request); |
| 38 | |
| 39 | if (response.ok) { |
| 40 | context.waitUntil(cache.put(request, createCacheableResponse(response))); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if (!response.ok) { |
| 45 | if (response.status === 404) { |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | throw new Error(`Failed to fetch file: ${response.status} ${response.statusText}`); |
| 50 | } |
| 51 | |
| 52 | return response; |
| 53 | } |
| 54 | |
| 55 | export async function getFile( |
| 56 | context: ExecutionContext, |
no test coverage detected