Skip to content

Commit

Permalink
experimental redirection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis Fedoruk-Betschki committed Oct 13, 2024
1 parent d29422f commit 03d8521
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,23 @@ proxy.on('proxyRes', (proxyRes, req, res) => {
console.log('Headers:', proxyRes.headers);
}

// Handle redirects (status codes 301, 302, 303, 307, 308)
if (proxyRes.statusCode && proxyRes.statusCode >= 300 && proxyRes.statusCode < 302 && proxyRes.headers.location) {
// Handle all redirects (including 302)
if (proxyRes.statusCode && proxyRes.statusCode >= 300 && proxyRes.statusCode < 400 && proxyRes.headers.location) {
console.log(`Handling redirect: ${proxyRes.statusCode} to ${proxyRes.headers.location}`);
res.writeHead(proxyRes.statusCode, {
'Location': proxyRes.headers.location
});

// Pass through the redirect response
res.writeHead(proxyRes.statusCode, proxyRes.headers);
res.end();
return;
}

// Ensure that we set the headers before streaming the response
// For non-redirected requests, pass through the response without buffering
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);

// Stream the response data directly to the client to avoid buffering large files in memory
proxyRes.pipe(res);

proxyRes.on('end', () => {
if (proxyRes.headers['x-cache-invalidate']) {
console.log(
'Detected x-cache-invalidate header, scheduling cache purge...'
);
console.log('Detected x-cache-invalidate header, scheduling cache purge...');
debouncePurgeCache();
}
});
Expand Down

0 comments on commit 03d8521

Please sign in to comment.