* Processes a list of routes and updates the URL if any match
( routes: Route[], url: URL, requestHeaders: Headers, responseHeaders: Headers, initialOrigin: string )
| 75 | * Processes a list of routes and updates the URL if any match |
| 76 | */ |
| 77 | function processRoutes( |
| 78 | routes: Route[], |
| 79 | url: URL, |
| 80 | requestHeaders: Headers, |
| 81 | responseHeaders: Headers, |
| 82 | initialOrigin: string |
| 83 | ): { |
| 84 | url: URL |
| 85 | externalRewrite?: URL |
| 86 | redirect?: { |
| 87 | url: URL |
| 88 | status: number |
| 89 | } |
| 90 | stopped: boolean |
| 91 | status?: number |
| 92 | } { |
| 93 | let currentUrl = url |
| 94 | let currentStatus: number | undefined |
| 95 | |
| 96 | for (const route of routes) { |
| 97 | const match = matchRoute(route, currentUrl, requestHeaders) |
| 98 | |
| 99 | if (match.matched) { |
| 100 | if (match.headers) { |
| 101 | for (const [key, value] of Object.entries(match.headers)) { |
| 102 | responseHeaders.set(key, value) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (route.status) { |
| 107 | currentStatus = route.status |
| 108 | } |
| 109 | |
| 110 | if (match.destination) { |
| 111 | // Check if route has redirect status and Location/Refresh header |
| 112 | if ( |
| 113 | isRedirectStatus(route.status) && |
| 114 | match.headers && |
| 115 | hasRedirectHeaders(match.headers) |
| 116 | ) { |
| 117 | const redirectUrl = isExternalDestination(match.destination) |
| 118 | ? new URL(match.destination) |
| 119 | : applyDestination(currentUrl, match.destination) |
| 120 | |
| 121 | return { |
| 122 | url: currentUrl, |
| 123 | redirect: { |
| 124 | url: redirectUrl, |
| 125 | status: route.status!, |
| 126 | }, |
| 127 | stopped: true, |
| 128 | status: currentStatus, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Check if it's an external rewrite |
| 133 | if (isExternalDestination(match.destination)) { |
| 134 | return { |
no test coverage detected