MCPcopy
hub / github.com/unpkg/unpkg / fetch

Function fetch

packages/unpkg-www/src/worker.ts:8–32  ·  view source on GitHub ↗
(request, env, context)

Source from the content-addressed store, hash-verified

6
7export default {
8 async fetch(request, env, context) {
9 try {
10 let url = new URL(request.url);
11 let shouldUseCache = url.pathname !== "/" && url.pathname !== "/index.html";
12 let response = shouldUseCache ? await cache.match(request) : undefined;
13
14 if (!response) {
15 response = await handleRequest(request, env, context);
16
17 if (shouldUseCache && request.method === "GET" && response.status === 200 && response.headers.has("Cache-Control")) {
18 context.waitUntil(cache.put(request, response.clone()));
19 }
20 }
21
22 if (request.method === "HEAD") {
23 return new Response(null, response);
24 }
25
26 return response;
27 } catch (error) {
28 console.error(error);
29
30 return new Response("Internal Server Error", { status: 500 });
31 }
32 },
33} satisfies ExportedHandler<Env>;

Callers 3

getPackageInfoFunction · 0.50
fetchFileFunction · 0.50
listFilesFunction · 0.50

Calls 1

handleRequestFunction · 0.90

Tested by

no test coverage detected