MCPcopy Create free account
hub / github.com/cloudflare/serverless-registry / fetch

Function fetch

index.ts:39–81  ·  view source on GitHub ↗
(request: Request, env: Env, context?: ExecutionContext)

Source from the content-addressed store, hash-verified

37
38export default {
39 async fetch(request: Request, env: Env, context?: ExecutionContext) {
40 if (!ensureConfig(env)) {
41 return new AuthErrorResponse(request);
42 }
43
44 const authMethod = await authenticationMethodFromEnv(env);
45 if (!authMethod) {
46 return new AuthErrorResponse(request);
47 }
48
49 const credentials = await authMethod.checkCredentials(request);
50 if (!credentials.verified) {
51 console.warn(`Not Authorized. authmode=${authMethod.authmode}. verified=false`);
52 return new AuthErrorResponse(request);
53 }
54
55 env.REGISTRY_CLIENT = new R2Registry(env);
56 try {
57 // Dispatch the request to the appropriate route
58 const res = await router.fetch(request, env, context);
59 return res;
60 } catch (err) {
61 if (err instanceof Response) {
62 console.warn(`${request.method} ${err.status} ${err.url}`);
63 return err;
64 }
65
66 // Unexpected error
67 if (err instanceof Error) {
68 console.error(
69 "An error has been thrown by the router:\n",
70 `${err.name}: ${err.message}: ${err.cause}: ${err.stack}`,
71 );
72 return new InternalError();
73 }
74
75 console.error(
76 "An error has been thrown and is neither a Response or an Error, JSON.stringify() =",
77 JSON.stringify(err),
78 );
79 return new InternalError();
80 }
81 },
82} satisfies ExportedHandler<Env>;
83
84const ensureConfig = (env: Env): boolean => {

Callers 11

pushLayerFunction · 0.50
index.tsFile · 0.50
authenticateMethod · 0.50
authenticateBearerMethod · 0.50
authenticateBasicMethod · 0.50
manifestExistsMethod · 0.50
getManifestMethod · 0.50
layerExistsMethod · 0.50
getLayerMethod · 0.50
listReferrersMethod · 0.50

Calls 3

ensureConfigFunction · 0.85
checkCredentialsMethod · 0.65

Tested by

no test coverage detected