(request: Request)
| 56 | } |
| 57 | |
| 58 | async function handler(request: Request) { |
| 59 | const contentLength = request.headers.get('content-length'); |
| 60 | const path = new URL(request.url).pathname; |
| 61 | |
| 62 | const isUploadRequest = path.startsWith('/api/files/upload') || path.startsWith('/dav'); |
| 63 | const maxSizeInBytes = isUploadRequest ? MAX_UPLOAD_SIZE_IN_BYTES : MAX_REQUEST_SIZE_IN_BYTES; |
| 64 | |
| 65 | if (contentLength && parseInt(contentLength, 10) > maxSizeInBytes) { |
| 66 | return new Response('Payload too large', { status: 413 }); |
| 67 | } |
| 68 | |
| 69 | const origin = request.headers.get('Origin') || '*'; |
| 70 | |
| 71 | // CORS headers for non-DAV routes |
| 72 | if ( |
| 73 | request.method == 'OPTIONS' && path !== '/dav' && !path.startsWith('/dav/') && path !== '/carddav' && |
| 74 | !path.startsWith('/carddav/') && path !== '/caldav' && !path.startsWith('/caldav/') |
| 75 | ) { |
| 76 | const response = new Response(null, { |
| 77 | status: 204, |
| 78 | }); |
| 79 | const headers = response.headers; |
| 80 | headers.set('Access-Control-Allow-Origin', origin); |
| 81 | headers.set('Access-Control-Allow-Credentials', 'true'); |
| 82 | headers.set('Access-Control-Allow-Methods', 'POST, OPTIONS, GET, PUT, DELETE'); |
| 83 | headers.set( |
| 84 | 'Access-Control-Allow-Headers', |
| 85 | 'Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With', |
| 86 | ); |
| 87 | return response; |
| 88 | } |
| 89 | |
| 90 | const routeKeys = Object.keys(routes); |
| 91 | |
| 92 | for (const routeKey of routeKeys) { |
| 93 | const route: Route = routes[routeKey]; |
| 94 | const match = route.pattern.exec(request.url); |
| 95 | |
| 96 | if (match) { |
| 97 | const response = await route.handler(request, match); |
| 98 | |
| 99 | applyCorsHeadersToResponse(origin, response); |
| 100 | handleLogging(request, response); |
| 101 | |
| 102 | return response; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | const notFoundPage: Page = (await import(`/pages/404.ts`)).default; |
| 107 | |
| 108 | const notFoundResponse = await notFoundPage.get!({ |
| 109 | request, |
| 110 | match: new URLPattern({ pathname: '/' }).exec(request.url) as URLPatternResult, |
| 111 | isRunningLocally: false, |
| 112 | }); |
| 113 | |
| 114 | const response = new Response(notFoundResponse.body, { |
| 115 | status: 404, |
no test coverage detected