Skip to content

Commit

Permalink
fix(server): 🐛 Return early if redirected
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <[email protected]>
  • Loading branch information
whizzzkid committed Oct 18, 2023
1 parent a981c12 commit 89c8854
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/heliaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class HeliaServer {
/**
* Checks if the request requires additional redirection.
*/
async requiresAdditionalRedirection ({ request, response }: IRouteHandler): Promise<void> {
async requiresAdditionalRedirection ({ request, response }: IRouteHandler): Promise<boolean> {
const {
namespace: reqNamespace,
relativePath,
Expand All @@ -128,9 +128,11 @@ export class HeliaServer {
const finalUrl = this.heliaFetch.sanitizeUrlPath(`${request.headers.referer}/${reqDomain}/${relativePath}`)
this.log('Redirecting to final URL:', finalUrl)
response.redirect(finalUrl)
return true
}
}
}
return false
}

/**
Expand All @@ -139,7 +141,10 @@ export class HeliaServer {
async fetch ({ request, response }: IRouteHandler): Promise<void> {
try {
await this.isReady
await this.requiresAdditionalRedirection({ request, response })
if (await this.requiresAdditionalRedirection({ request, response })) {
// the call needs redirection so we'll end it here.
return
}
this.log('Requesting content from helia:', request.path)
await this.fetchFromHeliaAndWriteToResponse({ response, request })
} catch (error) {
Expand Down

0 comments on commit 89c8854

Please sign in to comment.