MCPcopy
hub / github.com/unpkg/unpkg / handleRequest_

Function handleRequest_

packages/unpkg-files/src/lib/request-handler.ts:44–211  ·  view source on GitHub ↗
(request: Request)

Source from the content-addressed store, hash-verified

42}
43
44async function handleRequest_(request: Request): Promise<Response> {
45 let url = new URL(request.url);
46 let isInlineTransformRequest = url.pathname === "/transform" && request.method === "POST";
47
48 if (request.method === "OPTIONS") {
49 return new Response(null, {
50 headers: {
51 Allow: "GET, HEAD, OPTIONS, POST",
52 },
53 });
54 }
55 if (request.method !== "GET" && request.method !== "HEAD" && !isInlineTransformRequest) {
56 return new Response(`Invalid request method: ${request.method}`, { status: 405 });
57 }
58
59 if (url.pathname === "/_health") {
60 return new Response("OK");
61 }
62 if (url.pathname === "/favicon.ico") {
63 return notFound();
64 }
65
66 try {
67 if (isInlineTransformRequest) {
68 let body = await readInlineTransformRequest(request);
69 if ("response" in body) {
70 return body.response;
71 }
72
73 let result = await transformInlineEsmModule(publicNpmRegistry, {
74 filename: body.filename,
75 source: body.source,
76 options: normalizeBuildOptions(url.searchParams),
77 });
78
79 return new Response(result.code, {
80 headers: result.headers,
81 });
82 }
83
84 if (url.pathname.startsWith("/build")) {
85 let parsed = parsePackagePathname(url.pathname.slice(6));
86 if (parsed == null) {
87 return notFound(`Invalid build pathname: ${url.pathname}`);
88 }
89
90 let { package: packageName, version, filename } = parsed;
91
92 if (version == null) {
93 return notFound(`Missing version in pathname: ${url.pathname}`);
94 }
95 if (semver.clean(version) !== version) {
96 return notFound(`Invalid version: ${version}`);
97 }
98
99 let result = await buildEsmModule(publicNpmRegistry, {
100 packageName,
101 version,

Callers 1

handleRequestFunction · 0.85

Calls 8

transformInlineEsmModuleFunction · 0.90
normalizeBuildOptionsFunction · 0.90
buildEsmModuleFunction · 0.90
getFileFunction · 0.90
listFilesFunction · 0.90
notFoundFunction · 0.70
parsePackagePathnameFunction · 0.70

Tested by

no test coverage detected