MCPcopy
hub / github.com/unpkg/unpkg / fetchFile

Function fetchFile

packages/unpkg-worker/src/lib/npm-files.ts:20–53  ·  view source on GitHub ↗
(
  context: ExecutionContext,
  origin: string,
  packageName: string,
  version: string,
  filename: string
)

Source from the content-addressed store, hash-verified

18}
19
20export 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
55export async function getFile(
56 context: ExecutionContext,

Callers 2

handleRequestFunction · 0.90
getFileFunction · 0.85

Calls 2

createCacheableResponseFunction · 0.90
fetchFunction · 0.50

Tested by

no test coverage detected