(request: Request, env: Env, context?: ExecutionContext)
| 37 | |
| 38 | export 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 | |
| 84 | const ensureConfig = (env: Env): boolean => { |
no test coverage detected